Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worker Threads flag (node v10.5+) #676

Closed
alexcastillo opened this issue Sep 2, 2018 · 16 comments
Closed

Worker Threads flag (node v10.5+) #676

alexcastillo opened this issue Sep 2, 2018 · 16 comments

Comments

@alexcastillo
Copy link

Hi,

Thanks for all the work you've done with ts-node!

I'm currently trying to pass the --experimental-worker flag for Worker Threads support but it doesn't seem to be respecting the flag.

I've tried:

  • ts-node ./file.ts --experimental-worker
  • node --experimental-worker --require ts-node/register ./file.ts

And I get the following error:

  • error TS2307: Cannot find module 'worker_threads'.

Any ideas?

@blakeembrey
Copy link
Member

It’s not defined in the TypeScript definition for node.js yet.

@alexcastillo
Copy link
Author

Thanks for replying. Any chance you can give me more context on this?

Any ideas on where these definitions are located in the TypeScript project?

@blakeembrey
Copy link
Member

@alexcastillo
Copy link
Author

Thanks, @blakeembrey!

@jmgomez
Copy link

jmgomez commented Dec 4, 2018

Any idea of when they are going to be defined?

@blakeembrey
Copy link
Member

I don’t. You would have to add them yourself if you want them ASAP. It’s a community project to type third-party libraries.

@linusnorton
Copy link

linusnorton commented Dec 5, 2018

They're not usable via ts-node at the moment anyway. You get an error from node:

TypeError [ERR_WORKER_UNSUPPORTED_EXTENSION]: The worker script extension must be ".js" or ".mjs". Received ".ts"

@protectedvar
Copy link

protectedvar commented Jan 16, 2019

define NODE_OPTIONS as enviroment variable

`export NODE_OPTIONS=--experimental-worker

ts-node ./file.ts`

@maciejmatu
Copy link

If somebody wants to write thread workers as .ts files, it's possible by having an intermediate .js file that only acts as the importer. It also requires option allowJs: true in tsconfig.json

worker.import.js

require('ts-node').register();
require(path.resolve(__dirname, 'worker.ts'));

worker.ts

// regular ts file 

@marcpearson
Copy link

marcpearson commented Mar 14, 2019

@maciejmatu HI, i tried what you said in your comment for using ts-node with worker_threads but my node/ts-node app crash with this:

/vagrant/node_modules/rxjs/internal/util/hostReportError.js:4
setTimeout(function () { throw err; });

Exit code of the worker is 1

AllowJs is true in my tsconfig.json

My backend start command is:
node -r ts-node/register src/index.ts
Running : node 10.15.3 with experimental worker, ts-node 7.0.1

@maciejmatu
Copy link

@marcpearson not sure what exactly might be the issue in your build, but to expand on what I wrote previosuly:

I run the app using ts-node src/index.ts and in index.ts I initialize worker

const workerFile = process.env.NODE_ENV === 'production'
  ? 'worker.js'
  : 'worker.import.js';

  new Worker(path.resolve(__dirname, workerFile), { workerData });

In production the app will be compiled to js, so I just import worker.js file that will be there. In development on the other hand I need an intermediate file, that has .js file extension and just imports worker.ts

worker.import.js file

const path = require('path');

require('ts-node').register();
require(path.resolve(__dirname, './worker.ts'));

And the last file is worker.ts itself, where you have the worker logic.

@drachehavoc
Copy link

drachehavoc commented Sep 16, 2019

I made a different solution with a dynamic approach, I create an a function with a eval worker whos import the TS file.

check the code below:

import {
    Worker,
    isMainThread,
    parentPort,
    workerData,
    WorkerOptions
} from "worker_threads";

//
// WORKAROUND (GAMBIARRA)
//
const workerTs = (file: string, wkOpts: WorkerOptions) => {
    wkOpts.eval = true;
    if (!wkOpts.workerData) {
        wkOpts.workerData = {};
    }
    wkOpts.workerData.__filename = file;
    return new Worker(`
            const wk = require('worker_threads');
            require('ts-node').register();
            let file = wk.workerData.__filename;
            delete wk.workerData.__filename;
            require(file);
        `,
        wkOpts
    );
}

//
// MAIN FUNCTION
//
const main = () => {
    setInterval(() => console.log('main'), 1000);
    let wk = workerTs(__filename, {
        workerData: [1, 2, 3, 4, 5, 6, 7, 8, 9]
    });
    wk.on("online", () => console.log('Worker UP'));
    wk.on("message", (msg) => console.log('message:', msg));
    wk.on("exit", (code) => console.warn('exit', code));
    wk.on("error", (err) => console.error('error', err));
}

//
// THREAD FUNCTION
//
const thread = () => {
    if (!parentPort) return;
    console.log(workerData)
    parentPort.postMessage('asdas')
}

//
// RUN MAIN OR THREAD
//
isMainThread ? main() : thread();

ps: sorry for the bad english.

@liswin
Copy link

liswin commented Sep 7, 2020

This is a simple program that I am trying to run to find a factorial of a number using worker_threads and it throws me Error: Cannot find module 'worker_threads
node version: 10.5.0

index.ts

import { Worker } from 'worker_threads';
let workerData = {}
const worker = new Worker('./worker.js', { workerData } );

worker.on('message', (result:any) => {
    console.log(result);
});

worker.js

const path = require('path');
const { workerData } = require('worker_threads');
 
require('ts-node').register();
require(path.resolve(__dirname, workerData.path));`

worker.ts
`import { parentPort, workerData } from 'worker_threads';

function factorial(n: number): number {
    if (n === 1 || n === 0) {
        return 1;
    }
    return factorial(n - 1) * n;
}
if (parentPort) {
    parentPort.postMessage(
        factorial(workerData.value)
    );
}

I tried running index.ts with the command ts-node index.ts --experimental-worker, it throws me

Error: Cannot find module 'worker_threads'
tsconfig.js
{ "compilerOptions": { "target": "es5", "module": "commonjs", "allowJs": true, } }
May I know if I am missing something here ? @maciejmatu

@harrysolovay
Copy link

Why was this thread closed? Seems like it's still an issue.

@cspotcode
Copy link
Collaborator

@harrysolovay

It was closed because the error reported in this issue, error TS2307: Cannot find module 'worker_threads'., is caused by the reason explained in this comment: #676 (comment)

It’s not defined in the TypeScript definition for node.js yet.

This means that ts-node is behaving as intended, and the error is being correctly reported, as designed.

Is there any other information that might indicate that this problem is a ts-node issue, rather than an issue with @types/node?

@codeonmoon
Copy link

@maciejmatu HI, i tried what you said in your comment for using ts-node with worker_threads but my node/ts-node app crash with this:

/vagrant/node_modules/rxjs/internal/util/hostReportError.js:4 setTimeout(function () { throw err; });

Exit code of the worker is 1

AllowJs is true in my tsconfig.json

My backend start command is: node -r ts-node/register src/index.ts Running : node 10.15.3 with experimental worker, ts-node 7.0.1

@marcpearson - Is this issue resolved in your case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests