Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use flag `--experimental-worker` with babel-node

babel-node --experimental-worker app.js

Using this command for starting my project in development mode. Output is:

error: unknown option--experimental-worker'

config .babelrc:

{ "presets": [ ["env", { "targets": { "node": "current" } }], "stage-0", "flow" ], "plugins": [ "transform-decorators-legacy", "transform-flow-strip-types" ] }

This flag is needed to use worker threads. Using babel 6.26

like image 276
Николай Попов Avatar asked Nov 22 '25 12:11

Николай Попов


2 Answers

I just ran into this today and replied to the issue on GitHub here. I've pasted my fix below:

I was using Nodemon, and it turns out that there's an option to include environment variables as NODE_OPTIONS in the nodemon.json file. This did the trick for me:

{
  "watch": ["server"],
  "ext": "js",
  "env": {
    "NODE_OPTIONS": "--experimental-worker"
  },
  "exec": "babel-node server/server.js"
}

How to integrate Nodemon + worker_threads into a normal NodeJS app:

  1. Set up Nodemon
  2. Add a nodemon.json file to the root folder (example here)
  3. Add this to nodemon.json:

    "env": { "NODE_OPTIONS": "--experimental-worker" }

If you're setting up Nodemon for the first time, I found this tutorial very helpful.

like image 56
LiRa Avatar answered Nov 25 '25 10:11

LiRa


The idea is to split your command into two phases:

Phase 1:

babel app.js --out-file app-compiled.js

And phase 2:

node --experimental-worker app-compiled.js

In npm scripts you can then combine the two:

"scripts": {
  "pre-build": "babel ./app.js --out-file ./app-compiled.js",
  "serve": "yarn pre-build && node --experimental-worker ./app-compiled.js"    
}
like image 24
Oleg Kleiman Avatar answered Nov 25 '25 10:11

Oleg Kleiman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!