[英]Typescript cannot find module 'worker_threads'
I am trying to use node.js 'worker_threads' library with Node v14.15.1 and getting this error我正在尝试将 node.js 'worker_threads' 库与 Node v14.15.1 一起使用并收到此错误
Cannot find module 'worker_threads' or its corresponding type declarations.ts(2307)
tsc
src/api/services/email.service.ts:1:62 - error TS2307: Cannot find module 'worker_threads'.
1 import { Worker, isMainThread, parentPort, workerData } from 'worker_threads';
Found 1 error.
my code -我的代码 -
import { Worker, isMainThread, parentPort, workerData } from 'worker_threads';
worker threads got stable after node 11. So it should work.工作线程在节点 11 之后变得稳定。所以它应该可以工作。 What am i doing wrong?我究竟做错了什么?
PS - I ran same code with JS and it worked fine so i don't think its Node.js issue. PS - 我用 JS 运行了相同的代码,它运行良好,所以我不认为它的 Node.js 问题。 Other core libraries like fs are also working fine. fs 等其他核心库也运行良好。 Do I need any additional config for TS?我需要对 TS 进行任何额外的配置吗?
In my case, this problem happened in my Angular project after npm audit fix --force
就我而言,这个问题发生在我的Angular项目中,在npm audit fix --force
之后
So after few attempts here's what I did:因此,经过几次尝试,这就是我所做的:
sudo n stable
(you should install Node's version manager first)用sudo n stable
更新节点(你应该先安装节点的版本管理器)ng update
This command will give you few recommendations in a column named Command to update run each separately and make sure it worked correctly.此命令将在名为Command 的列中为您提供一些建议,以分别运行更新并确保其正常工作。
Then I run ng serve
and everything got back to normal.然后我运行ng serve
一切恢复正常。
use this package.json file with your ts file as index.ts
and the issue will be solved将此 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"
}
}
tasted on macOs bigSur 11.0.1在 macOS bigSur 11.0.1 上品尝
This issue was that I was using quite old versions.这个问题是我使用的是相当旧的版本。 After updating, the issue was resolved.更新后,问题得到解决。 If someone faces same issue, update these versions in your package.json - I have updated to如果有人遇到同样的问题,请在您的 package.json 中更新这些版本 - 我已更新为
Also make sure Nodejs version is 12 LTS or above to have stable 'worker-thread' support.还要确保 Nodejs 版本为 12 LTS 或更高版本,以获得稳定的“工作线程”支持。
After upgrading the node version to the latest v16.13.1, I was able to resolve the error.将节点版本升级到最新的 v16.13.1 后,我能够解决错误。 I also had to delete the node_modules folder and package-lock.json file.我还必须删除 node_modules 文件夹和 package-lock.json 文件。 I took Rohan's suggestion above and followed the instructions provided here:我接受了上面 Rohan 的建议,并按照此处提供的说明进行操作:
https://github.com/enable3d/enable3d/issues/231 https://github.com/enable3d/enable3d/issues/231
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.