Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node: bad option: --experimental-worker

I am trying to experiment with worker_threads in node.js.

const { Worker, isMainThread, parentPort } = require('worker_threads');
if (isMainThread) {
    // This code is executed in the main thread and not in the worker.

    // Create the worker.
    const worker = new Worker(__filename);
    // Listen for messages from the worker and print them.
    worker.on('message', (msg) => { console.log(msg); });
} else {
    // This code is executed in the worker and not in the main thread.

    // Send a message to the main thread.
    parentPort.postMessage('Hello world!');
}

I save the above code in index.js and run node --experimental-worker index.js on terminal. I get the following error:

node: bad option: --experimental-worker.

I have v8.16.0 of node installed in my mac.

like image 600
user3807454 Avatar asked Jan 26 '23 18:01

user3807454


1 Answers

The Worker class was added in nodejs v10.5.0.

To use --experimental-worker one will need at least nodejs v10.5.0

With nodejs v12.x it is stable and can be used without --experimental-worker

Downloads: nodejs

Version Manager: nvm

like image 77
Estradiaz Avatar answered Jan 28 '23 09:01

Estradiaz