pm2 not working with experimental-modules flag
Asked Answered
H

2

13

I am using ES6 modules by adding the --experimental-modules arguments to Node. Running node --experimental-modules app.mjs works perfectly fine. However, when I run the same command with pm2 I get the following error:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module

My current pm2 config file looks like this:

"apps": [
 {
  "name": "api",
  "script": "app.mjs",
  "exec_mode": "cluster",
  "instances": "max",
  "node_args": "--experimental-modules",
  "env": {
    variables here..
  }
 }
],

I have also tried using esm instead like this:

"node_args": "-r esm"

In both cases they return the same [ERR_REQUIRE_ESM] error


Does anyone have a solution on how to use es6 modules with pm2 or is it broken at the moment?

Hallowmas answered 25/9, 2018 at 13:44 Comment(2)
any solutions yet?Evictee
there is still an open issue in pm2 about this node flag github.com/Unitech/pm2/issues/3294Gilberto
C
9

UPDATE

Starting from Node.js version 12.14.1, you don't have to add the --experimental-modules argument, so if you update node, it should work without any changes to your configuration.

ORIGINAL ANSWER

A workaround for this problem:

  • In your package.json file, add a new script:

  "scripts": {
    //Add this one:
    "safestart":"node --experimental-modules app.mjs"
  },
  • Run this PM2 command:

pm2 start npm -- run safestart
Cataclinal answered 20/10, 2019 at 12:26 Comment(4)
please share how to run index.mjs with pm2. the sample above doesn't work for me.Mucronate
please check the update I provided for the answer @YuriyTigievCataclinal
I have installed [email protected] and nothing changedMucronate
With latest lts version v14.15.0 use "type": "module" in package.json and use ecosystem.config.cjs (Note the .cjs extension).Sciential
T
0

If you do not want to update node or another approach is using esm package

See my answer here.

Tuberculous answered 6/5, 2020 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.