3
votes

I have an application which uses esm modules and import statements. It runs fine in development using the --experimental-modules flag with nodemon, but when I run it in production using forever the imports fail because I don't know where to put the flag. When I run npm run build with the package.json as below it says sh: --experimental-modules: command not found. If I add --experimental-modules to the "script" part of forever-config.json it says the same thing.

I've searched the internet several times. How do use the --experimental-modules flag with forever? I'm running Node v10.16.0.

 {
    "type": "module",
    "name": "application_name",
    "version": "1.0.0",
    "private": true,
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "concurrently \"npm run build:dev\" \"npm run start-server:dev\"",
        "build": "npm run build:prod && npm run start-server:prod",
        "build:dev": "webpack --config webpack-dev.config.js",
        "build:prod": "webpack -p --config webpack-prod.config.js",
        "start-server:dev": "NODE_ENV=development nodemon --config nodemon.json --experimental-modules src/server/index.mjs ",
        "start-server:prod": "NODE_ENV=production --experimental-modules forever -o ./forever-out.log -e ./forever-error.log start forever-config.json"
    },
//rest of package.json left out for brevity
}

forever-config.json

{
    "uid": "application_name",
    "max": 5,
    "spinSleepTime": 1000,
    "minUptime": 1000,
    "append": true,
    "watch": false,
    "script": "src/server/index.mjs"
}
3

3 Answers

1
votes

I seem to been able to make it work using the command attribute where I pass this flag, e.g:

{
  "uid": "myApp",
  "max": 100,
  "spinSleepTime": 30000,
  "append": true,
  "watch": false,
  "command": "node --experimental-modules",
  "script": "./scripts/some-script.mjs",
  "path": "D:/MyProject",
  "workingDir": "./",
  "sourceDir": "./",
  "logFile": "./forever-script-log.log",
  "outFile": "./forever-script-out.log",
  "errFile": "./forever-script-err.log",
  "args": ["--someOpt", "someVal"]
}

Note: you can also use args to pass args to your script/app.

1
votes

In case you have no access to forever-config.json, you can run the command like this:

forever -c "node --experimental-modules" start app.js
0
votes

You can also set the NODE_OPTIONS environment variable in .bashrc or the like:

export NODE_OPTIONS="--experimental-modules"

(export seems to be required when using NVM)