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

Viewed 2920
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

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

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"    
}

It not actually for me already. I am refused to use nodemon and run my code with command

node --experimental-worker -r babel-register $NODE_DEBUG_OPTION app.js

It`s help me use exeprimental workers with babel, but with nodemon - not

Related