I have this simple code to try worker threads.
// thread.ts
import { Worker, isMainThread } from 'worker_threads';
if (isMainThread) {
const worker = new Worker(__filename);
} else {
console.log('hello from worker');
}
Apparently when I try to run the file with ts-node thread.ts
, i got this error:
(node:13200) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
SyntaxError: Cannot use import statement outside a module
But if I compile & invoke the file manually tsc && dist/thread.js
, it worked just fine
I try to experiment further, if I put the code that I want to run inside worker thread in an external file, then it worked just fine too
// thread.ts
import { Worker, isMainThread } from 'worker_threads';
const worker = new Worker('./src/worker.ts');
Here is my tsconfig.json
{
"compilerOptions": {
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"module": "commonjs", /* Specify what module code is generated. */
"rootDir": "./src", /* Specify the root folder within your source files. */
"outDir": "./dist", /* Specify an output folder for all emitted files. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}
Can anybody enlighten me?
Faced similar issu with launching .ts files as workers and here is how i managed to do this First of all, i use wrapper function to create a worker instance:
And my worker.ts looks like this:
Hope it will help you!