title: Is it possible to use Node worker threads to perform database inserts?
body: <p>I recently read about Node's &quot;worker_threads&quot; module that allows parallel execution of Javascript code in multiple threads which is useful for CPU-intensive operations. (NOTE: these are not web workers made by Chrome in the browser)</p>
  <p>I'm building a feature where I need to do a massive amount of Postgres INSERTs without blocking the browser.</p>
  <p><strong>The problem is:</strong> in my Javascript files where I instantiate the worker, I'm not allowed to import anything, including native Node modules or NPM libraries like Knex.js which is necessary to do database queries. I get an error that says: <strong>Cannot use import statement outside a module</strong> as soon as the file is executed.</p>
  <p>I've tried putting the worker code in another file with an import statement at the top (same error). I've tried giving the Knex object to workerData but it cannot clone a non-native JS object.</p>
  <p><em>I'm out of ideas- does anyone know how to interact with a database in a worker thread if we can't import any NPM libraries?!?!</em></p>
  <pre><code>// mainThread.js
  
  const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
  
  import knex from 'knex'; // --&gt; *** UNCAUGHT EXCEPTION: Cannot use import statement outside a module ***
  
  if (isMainThread) {
    module.exports = async function runWorker (rowsToInsert = []) {
      return new Promise((resolve, reject) =&gt; {
        const worker = new Worker(__filename, { workerData: { rowsToInsert } });
  
        worker.on('message', (returningRows) =&gt; resolve(returningRows));
        worker.on('error', reject);
        worker.on('exit', (code) =&gt; {
          if (code !== 0) reject(new Error(`Worker stopped with exit code ${code}`));
        });
      });
    };
  } else {
    const { rowsToInsert } = workerData;
  
    return knex('table').insert(rowsToInsert)
      .then((returningRows) =&gt; {
        parentPort.postMessage({ data: returningRows });
      });
  }
  </code></pre>
  <p>I am following a tutorial from this webpage: <a href="https://blog.logrocket.com/use-cases-for-node-workers/" rel="nofollow noreferrer">https://blog.logrocket.com/use-cases-for-node-workers/</a></p>