Nodemon抛出Typescript + Worker线程错误



我正在尝试添加一个工作线程到我的Typescript+node.js应用程序。

这是我的线程。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.

我试着替换"dev"script:"NODE_ENV=development nodemon src/index.ts"with"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

尝试使用

nodemon .

,告诉我它是否工作,它说什么

最新更新