node.js - Typescript + Worker Threads的Nodemon抛出错误
问题描述
我正在尝试向我的 Typescript+node.js 应用程序添加一个工作线程。
这是我的 thread.ts :
import {
Worker, isMainThread, parentPort, workerData
} from "worker_threads";
let threadFunction:Function = ()=>{};
if (isMainThread) {
threadFunction = function threadFunction() {
return new Promise((resolve, reject) => {
const worker = new Worker(__filename, {
workerData: "abc"
});
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0)
reject(new Error(`Worker stopped with exit code ${code}`));
});
});
};
} else {
console.log("Received Data from Parent : ",workerData);
}
export default threadFunction;
这是节点应用程序入口文件(index.ts):
import threadFunction from "./workers/thread"
threadFunction();
这些是我的package.json
脚本:
"scripts": {
"start": "NODE_ENV=production node dist",
"dev": "NODE_ENV=development nodemon src/index.ts",
"test": "jest --watch --all --runInBand",
"build": "tsc -p ."
}
现在npm run build
&npm start
正在编译和执行,即使在添加新线程之后也是如此。问题在于我的npm run dev
脚本引发了错误:
(node:2576830) UnhandledPromiseRejectionWarning: TypeError [ERR_WORKER_UNSUPPORTED_EXTENSION]: The worker script extension must be ".js", ".mjs", or ".cjs". Received ".ts"
at new Worker (internal/worker.js:150:15)
at /path-to-my-ts-project/src/workers/thread.ts:9:28
at new Promise (<anonymous>)
at Object.threadFunction [as default] (/path-to-my-ts-project/src/workers/thread.ts:8:16)
at Object.<anonymous> (/path-to-my-ts-project/src/index.ts:38:14)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Module.m._compile (/path-to-my-ts-project/node_modules/ts-node/src/index.ts:1043:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Object.require.extensions.<computed> [as .ts] (/path-to-my-ts-project/node_modules/ts-node/src/index.ts:1046:12)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at main (/path-to-my-ts-project/node_modules/ts-node/src/bin.ts:225:14)
at Object.<anonymous> (/path-to-my-ts-project/node_modules/ts-node/src/bin.ts:512:3)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:2576830) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:2576830) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我尝试替换“开发”脚本"NODE_ENV=development nodemon src/index.ts"
:"NODE_ENV=development nodemon --watch \"src/**\" --ext \"ts,json\" --ignore \"src/**/*.test.ts\" --exec \"ts-node src/index.ts\""
我仍然收到类似的错误:
(node:2577565) UnhandledPromiseRejectionWarning: TypeError [ERR_WORKER_UNSUPPORTED_EXTENSION]: The worker script extension must be ".js", ".mjs", or ".cjs". Received ".ts"
at new Worker (internal/worker.js:150:15)
at /path-to-my-ts-project/src/workers/thread.ts:9:28
at new Promise (<anonymous>)
at Object.threadFunction [as default] (/path-to-my-ts-project/src/workers/thread.ts:8:16)
at Object.<anonymous> (/path-to-my-ts-project/src/index.ts:38:14)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Module.m._compile (/path-to-my-ts-project/node_modules/ts-node/src/index.ts:1043:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Object.require.extensions.<computed> [as .ts] (/path-to-my-ts-project/node_modules/ts-node/src/index.ts:1046:12)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at main (/path-to-my-ts-project/node_modules/ts-node/src/bin.ts:225:14)
at Object.<anonymous> (/path-to-my-ts-project/node_modules/ts-node/src/bin.ts:512:3)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:2577565) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:2577565) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
请帮忙!
仅供参考:
"devDependencies": {
"@types/node": "^14.6.4",
"nodemon": "^2.0.4",
"ts-node": "^9.0.0",
"typescript": "^4.0.2"
}
&node.js:v14.17.5
解决方案
尝试使用
节点监视器。
在您的控制台中并告诉我它是否有效以及它说什么
推荐阅读
- git - 仅针对特定位置运行 mkdir 时 Dockerfile 构建失败
- python - python3中的静态类型:list vs List
- c - 持续电源功能消息
- ios - 在 Obj-C 中动态进行 segue 的最有效方法?
- sql - 如何将 XML 行转换为列
- java - 在销毁“无法访问”对象之前,垃圾收集器是否总是调用“finalize()”方法?
- python - Python 函数,如何从 SQL 语句和两个 DictReader 插入数据库
- google-api-java-client - 谷歌地图 API 重叠方向显示
- javascript - 如何计算基础表 td 上的项目数?
- image - 在 CKEditor 5 中设置图像的宽度