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?
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:
"scripts": {
//Add this one:
"safestart":"node --experimental-modules app.mjs"
},
pm2 start npm -- run safestart
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With