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
Comments
|
It’s not defined in the TypeScript definition for node.js yet. |
|
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? |
|
When you installed |
|
Thanks, @blakeembrey! |
|
Any idea of when they are going to be defined? |
|
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. |
|
They're not usable via ts-node at the moment anyway. You get an error from node: |
|
define NODE_OPTIONS as enviroment variable `export NODE_OPTIONS=--experimental-worker ts-node ./file.ts` |
|
If somebody wants to write thread workers as worker.import.js worker.ts |
|
@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 Exit code of the worker is 1 AllowJs is true in my tsconfig.json My backend start command is: |
|
@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 In production the app will be compiled to js, so I just import
And the last file is |
|
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. |
|
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 index.ts worker.js I tried running index.ts with the command
|
|
Why was this thread closed? Seems like it's still an issue. |
|
It was closed because the error reported in this issue,
This means that Is there any other information that might indicate that this problem is a ts-node issue, rather than an issue with @types/node? |
@marcpearson - Is this issue resolved in your case? |
Hi,
Thanks for all the work you've done with ts-node!
I'm currently trying to pass the
--experimental-workerflag for Worker Threads support but it doesn't seem to be respecting the flag.I've tried:
ts-node ./file.ts --experimental-workernode --experimental-worker --require ts-node/register ./file.tsAnd I get the following error:
error TS2307: Cannot find module 'worker_threads'.Any ideas?
The text was updated successfully, but these errors were encountered: