Typescript + Worker Threads的Nodemon抛出错误

Posted

技术标签:

【中文标题】Typescript + Worker Threads的Nodemon抛出错误【英文标题】:Nodemon throwing error with Typescript + Worker Threads 【发布时间】:2021-11-20 06:01:26 【问题描述】:

我正在尝试向我的 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 buildnpm 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

【问题讨论】:

this 有帮助吗? 这行得通,谢谢 :) .. 但我希望在 nodemon 或 ts-node 中有一个 switch 标志。 【参考方案1】:

尝试使用

节点监视器。

在你的控制台中告诉我它是否有效以及它说了什么

【讨论】:

什么也没做。

以上是关于Typescript + Worker Threads的Nodemon抛出错误的主要内容,如果未能解决你的问题,请参考以下文章

TypeScript入门知识五(面向对象特性二)

WebPack 5 与使用 React/Typescript 的 Web 工作者

Kafka消费者客户端心跳请求

Kafka消费者客户端心跳请求

Kafka消费者客户端心跳请求

如何理解celery中的worker并发和多worker