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 · 11 comments

Comments

Projects
None yet
7 participants
@alexcastillo
Copy link

commented Sep 2, 2018

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

This comment has been minimized.

Copy link
Member

commented Sep 2, 2018

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

@blakeembrey blakeembrey closed this Sep 2, 2018

@alexcastillo

This comment has been minimized.

Copy link
Author

commented Sep 2, 2018

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

This comment has been minimized.

Copy link
Member

commented Sep 2, 2018

@alexcastillo

This comment has been minimized.

Copy link
Author

commented Sep 2, 2018

Thanks, @blakeembrey!

@jmgomez

This comment has been minimized.

Copy link

commented Dec 4, 2018

Any idea of when they are going to be defined?

@blakeembrey

This comment has been minimized.

Copy link
Member

commented Dec 5, 2018

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Jan 16, 2019

define NODE_OPTIONS as enviroment variable

`export NODE_OPTIONS=--experimental-worker

ts-node ./file.ts`

@maciejmatu

This comment has been minimized.

Copy link

commented Mar 8, 2019

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Mar 14, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.