我正在尝试在 node.js
中使用 worker_threads。
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!');
}
我将以上代码保存在 index.js
中,并在终端上运行 node --experimental-worker index.js
。
我收到以下错误:
node: bad option: --experimental-worker.
我在我的 mac 中安装了 Node 的 v8.16.0
。
最佳答案
Worker
nodejs v10.5.0 中添加了类。
要使用 --experimental-worker
至少需要 nodejs v10.5.0
与 nodejs v12.x稳定,无需--experimental-worker
下载:nodejs
版本管理器:nvm
关于javascript - Node : bad option: --experimental-worker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58154066/