我正在尝试在 Node v14.15.1 中使用 node.js 的 'worker_threads' 库,但是遇到了以下错误:
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.
我的代码 -
import { Worker, isMainThread, parentPort, workerData } from 'worker_threads';
自 Node 11 版本以后,工作线程就变得稳定了。因此,应该可以正常工作。
附注:我用 JS 运行相同的代码,它可以正常工作,因此我认为这不是 Node.js 的问题。其他核心库(如 fs)也正常工作。我需要为 TypeScript 进行任何额外的配置吗?
npm install @types/node
命令,或者使用您选择的软件包管理器执行相应的等效命令。 - Aluan Haddad