Nodemon throwing error with Typescript + Worker Threads

Viewed 675

I am trying to add a worker thread to my Typescript+node.js application.

This is my 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;

This is node app entry file(index.ts) :

import threadFunction from "./workers/thread"
threadFunction();

these are my package.json scripts:

"scripts": {
    "start": "NODE_ENV=production node dist",
    "dev": "NODE_ENV=development nodemon src/index.ts",
    "test": "jest --watch --all --runInBand",
    "build": "tsc -p ."
  }

Now npm run build & npm start are compiling & executing fine even after adding the new thread. Problem is with my npm run dev script that throws the error :

(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.

I tried replacing the "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\""

I still get a similar error :

(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.

Please help!


FYI:

"devDependencies": {
    "@types/node": "^14.6.4",
    "nodemon": "^2.0.4",
    "ts-node": "^9.0.0",
    "typescript": "^4.0.2"
  }

& node.js : v14.17.5

1 Answers

Try using

nodemon .

in your console and tell me if it works or not and what it says

Related