[ERR_REQUIRE_ESM]: Must use import to load module
What's going wrong?
I am not able to start node application with pm2. The error shown in the log is:
[C:\test\src\index.mjs:1 Error [ERR_REQUIRE_ESM]: Must use import to load module: file:///C:/test/src/index.mjs]
How could we reproduce this issue?
Node v12.18.2 [email protected] installed with yarn global add pm2
package.json
{
"name": "test",
"main": "src/index.mjs",
"dependencies": {
"esm": "^3.2.25"
}
}
service.config.js
module.exports = {
/**
* Application configuration section
* http://pm2.keymetrics.io/docs/usage/application-declaration/
*/
apps: [{
name: 'test',
script: './src/index.mjs',
node_args: '-r esm',
watch: false,
}],
}
index.mjs
const startConsuming = async () => {
console.log(1);
}
startConsuming()
export default startConsuming;
$ pm2 report
Start command: pm2 start src/index.mjs --node-args="-r esm" The app works fine if it's being started with node -r esm src/index.mjs
I have the same issue. It is super weird. how we can fix it?
any update? I meet same problem node index.js works fine, but pm2 start doesn't
pm2 5.1.2 node 12.18.1 package.json type:"module"
in my case I've solved creating a ecosystem.config.cjs (note the extension file is .cjs), and running
pm2 start ecosystem.config.cjs
module.exports = {
apps: [{
script: "app.js",
instances: "max",
exec_mode: "cluster"
}]
}
Tried upgrading pm2 and nodejs (from 12 to 16) - didn't help. It started working for me after I changed the file extension on all project files from .mjs to .js.
pm2 start ecosystem.config.js
module.exports = {
apps: [{
name: 'test',
script: './src/index.js',
node_args: '-r esm'
}]
}