Pm2: --experimental-modules flag appears to be ignored

Created on 20 Nov 2017  ยท  19Comments  ยท  Source: Unitech/pm2

I'm using the node_args section as follows to enable module support in a Node v9.1 project:

"node_args": "--experimental-modules"

It appears to ignore the flag, as starting my script manually using the above works perfectly. Is there something I'm doing wrong?

Need More Data

Most helpful comment

Hey @Unitech, --experimental-modules appears to still be failing.

module.exports = {
  apps: [
    {
      name: 'MyApp',
      script: 'myapp.mjs',
      node_args: '--experimental-modules',
      env: {
        NODE_ENV: 'production'
      },
    },
  ],
};

node v10.6.0. The error log shows that node didn't receive the --experimental-modules flag:

/home/dandv/myapp.mjs:7
import { localISOdt } from 'local-iso-dt';
       ^^^^^^^^^^^

SyntaxError: Unexpected identifier

All 19 comments

We added a test case on our CI (here) and its works great, are you trying to add it when the process already exist ? Also can you run pm2 report and paste the result here ?

I've tried multiple different formats and each time I make sure to execute pm2 kill first.

Requested Output:

===============================================================================
--- PM2 REPORT (Mon Nov 20 2017 15:00:09 GMT+0000 (UTC)) ----------------------
===============================================================================
--- Daemon -------------------------------------------------
pm2d version         : 2.7.2
node version         : 9.1.0
node path            : /home/ubuntu/.nvm/versions/node/v9.1.0/bin/pm2
argv                 : /home/ubuntu/.nvm/versions/node/v9.1.0/bin/node,/home/ubuntu/.nvm/versions/node/v9.1.0/lib/node_modules/pm2/lib/Daemon.js
argv0                : node
user                 : ubuntu
uid                  : 1000
gid                  : 1000
uptime               : 0min
===============================================================================
--- CLI ----------------------------------------------------
local pm2            : 2.7.2
node version         : 9.1.0
node path            : /home/ubuntu/.nvm/versions/node/v9.1.0/bin/pm2
argv                 : /home/ubuntu/.nvm/versions/node/v9.1.0/bin/node,/home/ubuntu/.nvm/versions/node/v9.1.0/bin/pm2,report
argv0                : node
user                 : ubuntu
uid                  : 1000
gid                  : 1000
===============================================================================
--- System info --------------------------------------------
arch                 : x64
platform             : linux
type                 : Linux
cpus                 : Intel(R) Xeon(R) CPU E5-2686 v4 @ 2.30GHz
cpus nb              : 8
freemem              : 31827783680
totalmem             : 33736904704
home                 : /home/ubuntu
===============================================================================
--- PM2 list -----------------------------------------------
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ App name โ”‚ id โ”‚ mode โ”‚ pid โ”‚ status โ”‚ restart โ”‚ uptime โ”‚ cpu โ”‚ mem โ”‚ user โ”‚ watching โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
===============================================================================
--- Daemon logs --------------------------------------------
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ App name โ”‚ id โ”‚ mode โ”‚ pid โ”‚ status โ”‚ restart โ”‚ uptime โ”‚ cpu โ”‚ mem โ”‚ user โ”‚ watching โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

we made a test here:
https://github.com/Unitech/pm2/blob/f6c70529bbc04c0e1340e519eddb1534b952c438/test/fixtures/mjs/ecosystem.config.js

to support import syntax. As you can see the flag --experimental-modulesis taken into account.

Please start this script and see what happens. Make sur you've upgraded to PM2 2.8

@seanlindo did you manage to solve? Strangely pm2 stopped to work properly with --experimental-modules flag and node v10.5. No errors at all, it looks like the entrypoint is ignored and not executed at all.

Hey @Unitech, --experimental-modules appears to still be failing.

module.exports = {
  apps: [
    {
      name: 'MyApp',
      script: 'myapp.mjs',
      node_args: '--experimental-modules',
      env: {
        NODE_ENV: 'production'
      },
    },
  ],
};

node v10.6.0. The error log shows that node didn't receive the --experimental-modules flag:

/home/dandv/myapp.mjs:7
import { localISOdt } from 'local-iso-dt';
       ^^^^^^^^^^^

SyntaxError: Unexpected identifier

I can`t run pm2 with --experimental-modules :(

Try to downgrade node version to 8.5.

Node 10.10 and pm2 3.1.2, also experiencing this issue.

I am using esm loader and my application (with experimental modules on node 10) works perfectly.

So, try this:

ecosystem.config.js:

module.exports = {
  apps: [
    {
      name: 'My super application',
      script: './src/index.js',
      node_args: '-r esm --experimental-modules',
    },
  ],
};
$ npm i -g esm pm2
$ pm2 start ecosystem.config.js

Be careful, extension must be a .js only (not .mjs).

Another way of working around the issue is to use a wrapper like the following so that pm2 will load a standard .js file (--experimental-modules is still needed in node_args):

// main.js
//
// Wrapper for es6 import compatibility
//

async function main() {
  await import('./index.mjs')
}
main()

Now that node 12 has been released and plans to make .mjs support official in the October release, it would be nice if pm2 supported .mjs files natively, without wrappers or hacky workarounds.

@dandv I am almost positive that this will be natively supported because one way to run .mjs files is to write

pm2 start node --name xxx -- --experimental-modules script.mjs

(note : pm2 forwards the arguments after the -- to the process used by the start command.)

That means if node doesn't require the experimental flag anymore the execution would become

pm2 start node --name xxx -- script.mjs
# even better
pm2 start --name xxx ./scripts.mjs

PM2: 3.5.0
NodeJS: 12.2.0

Still doesn't work :(

npx pm2 start src/index.js --node-args="--experimental-modules --experimental-json-modules"
0|index    | import fetch from 'node-fetch';
0|index    |        ^^^^^
0|index    |
0|index    | SyntaxError: Unexpected identifier

Why is this issue closed? It still doesn't work. My webserver correctly works with
node index.js --experimental-modules
but fails with
npx pm2 start index.js --node-args="--experimental-modules"

pm2: 3.5.1
node js: 12.5.0

@bifot,

I am using esm loader and my application (with experimental modules on node 10) works perfectly.

So, try this:

ecosystem.config.js:

module.exports = {
  apps: [
    {
      name: 'My super application',
      script: './src/index.js',
      node_args: '-r esm --experimental-modules',
    },
  ],
};
$ npm i -g esm pm2
$ pm2 start ecosystem.config.js

Be careful, extension must be a .js only (not .mjs).

Here, -esm just takes over the task, --experimental-modules has no effect, you can omit it and it still works. This suggests that pm2 actually takes the args into account, but maybe package.json is not seeable for node, from which it could read out package.type: "module"

Also, npm i -g esm doesn't work for me, but npm i esm does. Maybe because pm2 is also not global at me. The config file is also not crucial, npx pm2 index.js --node-args="-r esm" just works.

I have the same problem. Here is the simple example. Look its readme.md file, please. How can I solve the problem?

P.S. Why this issue is closed while the problem exists?

I am using PM2 v3.1.2 and cannot start an app with --experimental-worker flag. Any updates on this? Should I upgrade Node to v12 so I don't need the flag at all and PM2 will pick it up naturally?

we made a test here:
https://github.com/Unitech/pm2/blob/f6c70529bbc04c0e1340e519eddb1534b952c438/test/fixtures/mjs/ecosystem.config.js

to support import syntax. As you can see the flag --experimental-modulesis taken into account.

Please start this script and see what happens. Make sur you've upgraded to PM2 2.8

I've done it (on master branch) and I have a failure (with nodejs v11.15.0, pm2 v4.2.1):

alex@omen17:~/work/pm2/test/fixtures/mjs$ pm2 start ecosystem.config.js 
[PM2][WARN] Applications Test es6 modules not running, starting...
[PM2] App [Test es6 modules] launched (1 instances)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ id  โ”‚ name                โ”‚ namespace   โ”‚ version โ”‚ mode    โ”‚ pid      โ”‚ uptime โ”‚ โ†บ    โ”‚ status    โ”‚ cpu      โ”‚ mem      โ”‚ user     โ”‚ watching โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 1   โ”‚ Test es6 modules    โ”‚ default     โ”‚ 1.0.0   โ”‚ fork    โ”‚ 20704    โ”‚ 0s     โ”‚ 0    โ”‚ online    โ”‚ 0%       โ”‚ 21.2mb   โ”‚ alex     โ”‚ disabled โ”‚
โ”‚ 0   โ”‚ app_es6             โ”‚ default     โ”‚ 0.1.0   โ”‚ fork    โ”‚ 0        โ”‚ 0      โ”‚ 15   โ”‚ errored   โ”‚ 0%       โ”‚ 0b       โ”‚ alex     โ”‚ disabled โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
[PM2][WARN] Current process list running is not in sync with saved list. app_cjs differs. Type 'pm2 save' to synchronize.
alex@omen17:~/work/pm2/test/fixtures/mjs$ pm2 log
[TAILING] Tailing last 15 lines for [all] processes (change the value with --lines option)
/home/alex/.pm2/pm2.log last 15 lines:
PM2        | 2019-12-28T00:03:11: PM2 log: App [Test es6 modules:1] online
PM2        | 2019-12-28T00:03:11: PM2 log: App [Test es6 modules:1] exited with code [1] via signal [SIGINT]
PM2        | 2019-12-28T00:03:11: PM2 log: App [Test es6 modules:1] starting in -fork mode-
PM2        | 2019-12-28T00:03:11: PM2 log: App [Test es6 modules:1] online
PM2        | 2019-12-28T00:03:12: PM2 log: App [Test es6 modules:1] exited with code [1] via signal [SIGINT]
PM2        | 2019-12-28T00:03:12: PM2 log: App [Test es6 modules:1] starting in -fork mode-
PM2        | 2019-12-28T00:03:12: PM2 log: App [Test es6 modules:1] online
PM2        | 2019-12-28T00:03:12: PM2 log: App [Test es6 modules:1] exited with code [1] via signal [SIGINT]
PM2        | 2019-12-28T00:03:12: PM2 log: App [Test es6 modules:1] starting in -fork mode-
PM2        | 2019-12-28T00:03:12: PM2 log: App [Test es6 modules:1] online
PM2        | 2019-12-28T00:03:12: PM2 log: App [Test es6 modules:1] exited with code [1] via signal [SIGINT]
PM2        | 2019-12-28T00:03:12: PM2 log: App [Test es6 modules:1] starting in -fork mode-
PM2        | 2019-12-28T00:03:12: PM2 log: App [Test es6 modules:1] online
PM2        | 2019-12-28T00:03:12: PM2 log: App [Test es6 modules:1] exited with code [1] via signal [SIGINT]
PM2        | 2019-12-28T00:03:12: PM2 log: Script /home/alex/work/pm2/test/fixtures/mjs/index.mjs had too many unstable restarts (16). Stopped. "errored"

...

/home/alex/.pm2/logs/Test-es6-modules-out.log last 15 lines:
/home/alex/.pm2/logs/Test-es6-modules-error.log last 15 lines:
1|Test es6 |     at Function.Module._load (internal/modules/cjs/loader.js:620:12)
1|Test es6 |     at createDynamicModule (internal/modules/esm/translators.js:81:15)
1|Test es6 |     at Object.meta.done (internal/modules/esm/create_dynamic_module.js:40:9)
1|Test es6 | (node:20906) ExperimentalWarning: The ESM module loader is experimental.
1|Test es6 | Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/alex/work/pm2/test/fixtures/mjs/index.mjs
1|Test es6 |     at Object.Module._extensions..mjs (internal/modules/cjs/loader.js:862:11)
1|Test es6 |     at Module.load (internal/modules/cjs/loader.js:685:32)
1|Test es6 |     at Proxy.Module._load (internal/modules/cjs/loader.js:620:12)
1|Test es6 |     at Object.<anonymous> (/usr/lib/node_modules/pm2/lib/ProcessContainerFork.js:27:21)
1|Test es6 |     at Module._compile (internal/modules/cjs/loader.js:816:30)
1|Test es6 |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
1|Test es6 |     at Module.load (internal/modules/cjs/loader.js:685:32)
1|Test es6 |     at Function.Module._load (internal/modules/cjs/loader.js:620:12)
1|Test es6 |     at createDynamicModule (internal/modules/esm/translators.js:81:15)
1|Test es6 |     at Object.meta.done (internal/modules/esm/create_dynamic_module.js:40:9)

So, in my case everything works ok with esm as long as I do not use services.

I am running on Raspberry Pi 4.

After I do sudo systemctl start pm2-pi.service I get this in the report:

Error: Cannot find module 'esm'
PM2        | Require stack:
PM2        | - internal/preload
PM2        |     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:793:17)
PM2        |     at Function.Module._load (internal/modules/cjs/loader.js:686:27)
PM2        |     at Module.require (internal/modules/cjs/loader.js:848:19)
PM2        |     at Module._preloadModules (internal/modules/cjs/loader.js:1133:12)
PM2        |     at loadPreloadModules (internal/bootstrap/pre_execution.js:443:5)
PM2        |     at prepareMainThreadExecution (internal/bootstrap/pre_execution.js:62:3)
PM2        |     at internal/main/run_main_module.js:7:1 {
PM2        |   code: 'MODULE_NOT_FOUND',
PM2        |   requireStack: [ 'internal/preload' ]
PM2        | }

When I stop service (sudo systemctl stop pm2-pi.service), then pm2 resurrect seem to be occasionally working - sometimes I need to do pm2 unstartup systemd and do a couple of reboots for things to become normal again and sometimes only pm2 start all helps. Anyway, troubles seem to begin with enabling services.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rangercyh picture rangercyh  ยท  4Comments

xzycn picture xzycn  ยท  3Comments

ghost picture ghost  ยท  3Comments

ldarren picture ldarren  ยท  3Comments

alexpts picture alexpts  ยท  3Comments