javascript - Node : bad option: --experimental-worker

标签 javascript node.js worker-thread

我正在尝试在 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/

相关文章:

javascript - 由于范围问题,变量返回为未定义

javascript - Node.js 不支持 ES6 导入和导出

c# - 拥有无限工作线程的正确方法?

c# - 使用优先级队列时工作线程性能降低

javascript - 长时间保持的 AJAX 连接被防病毒软件阻止

javascript - 为什么在react-native-router-flux中导航栏后退按钮仍然显示为ActionConst.REPLACE?

javascript - 如何显示response.data中的单个对象?

javascript - 当用户在不同的选项卡中时,jQuery 更改页面的标题

javascript - 无法使用 .update() 更新 Mongoose 属性

android - 最多可以创建多少个工作线程?