Node.js Typescript找不到模块';工作线程';

Node.js Typescript找不到模块';工作线程';,node.js,typescript,node-worker-threads,Node.js,Typescript,Node Worker Threads,我试图将node.js“worker_threads”库与节点v14.15.1一起使用,但出现了此错误 Cannot find module 'worker_threads' or its corresponding type declarations.ts(2307) 我的代码- import { Worker, isMainThread, parentPort, workerData } from 'worker_threads'; 工作线程在节点11之后变得稳定。所以它应该会起作用。

我试图将node.js“worker_threads”库与节点v14.15.1一起使用,但出现了此错误

Cannot find module 'worker_threads' or its corresponding type declarations.ts(2307)
我的代码-

import { Worker, isMainThread, parentPort, workerData } from 'worker_threads';
工作线程在节点11之后变得稳定。所以它应该会起作用。 我做错了什么


PS-我用JS运行了相同的代码,它运行得很好,所以我认为它的Node.JS没有问题。像fs这样的其他核心库也运行良好。我需要TS的任何其他配置吗?

将此package.json文件与您的TS文件一起用作
index.TS
这个问题将会得到解决

{
  "name": "threads-worshop",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start:build": "tsc -w",
    "start:run": "nodemon build/index.js",
    "start": "concurrently npm:start:*"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/node": "^14.14.10",
    "concurrently": "^5.3.0",
    "nodemon": "^2.0.6"
  }
}

在macOs bigSur 11.0.1上试用过这个问题是因为我使用的是非常旧的版本。更新后,问题得到解决。 如果有人面临相同的问题,请在package.json中更新这些版本- 我已经更新到

  • @types/node:“14.14.14”
  • “ts节点”:“9.1.1”

还要确保Nodejs版本为12 LTS或更高版本,以获得稳定的“工作线程”支持。

在我的情况下,此问题发生在我的Angular项目中
npm audit fix--force之后

因此,经过几次尝试后,以下是我所做的:

  • 使用
    sudo n stable
    更新节点(应首先安装节点的版本管理器)

  • ng更新
此命令将在名为的列中为您提供一些建议,以更新分别运行每个命令并确保其正常工作


然后我运行了
ng serve
,一切都恢复正常。

npm install@types/node
或您选择的软件包管理器的等效命令。谢谢,但已经这样做了,其他核心库(如“fs”)工作正常。只需使用此库验证@types包的版本是否正确correct@AluanHaddad谢谢你的帮助。我不记得为什么,但我有非常旧版本的@types/node和ts node。更新后效果很好。再次感谢@RohanGulati我也有同样的问题,你更新了@types/node和ts node的哪些版本?如果您可以将这些步骤指定为答案或注释,则会有所帮助。对于“ts节点”:“^9.1.1”和“@types/node”:“^14.14.37”。@EugeneZalivadnyi这在这些版本中经过了很好的测试。你面临什么问题?也许我能帮忙
{
  "name": "threads-worshop",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start:build": "tsc -w",
    "start:run": "nodemon build/index.js",
    "start": "concurrently npm:start:*"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/node": "^14.14.10",
    "concurrently": "^5.3.0",
    "nodemon": "^2.0.6"
  }
}