Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PM2 with ES module. Error: ERR_REQUIRE_ESM

My package.json file looks something like this:

{
...
  "main": "index.js",
  "type": "module",
  "scripts": {
    "devStart": "pm2 start ecosystem.config.js --env dev --watch",
    "prodStart": "pm2 start ecosystem.config.js --env prod --watch",
    "reload": "pm2 reload ecosystem.config.js",
    "stop": "pm2 stop ecosystem.config.js",
    "end": "pm2 delete ecosystem.config.js"
  },
...
}

I have activated ES modules by "type": "module", as you see.

And the ecosystem.config.js file:

module.exports = {
  apps : [{
    name   : "app1",
    script : "./app.js",
    env_production: {
       NODE_ENV: "production"
    },
    env_development: {
       NODE_ENV: "development"
    }
  }]
}

So, when I run the script npm run devStart an error occurs.

File ecosystem.config.js malformated

code: 'ERR_REQUIRE_ESM'

It works when I just remove the "type": "module" part from config file.

How can I solve this?

node -v : v16.13.0

pm2 -v : 5.1.2

like image 815
Sha'an Avatar asked Jul 14 '26 01:07

Sha'an


2 Answers

Rename ecosystem.config.js to ecosystem.config.cjs

like image 175
Arifuzzaman Nishan Avatar answered Jul 16 '26 13:07

Arifuzzaman Nishan


I guess that the better way to avoid this error is creating the ecosystem.config.js outside the app directory that PM2 couldn't see the package.json file. I could run my ecosystem.config only use this way and also fixed a path for running script. Example :

module.exports = { 
 apps : [{
    name   : "nuxt3_app",
    script : "myApp/.output/server/index.mjs",
    interpreter: "node",
    env: {
       DATABASE_URL: "mysql://root:root@localhost:3306/db"
    },

  }]
}
like image 25
Zerg Zerg Avatar answered Jul 16 '26 13:07

Zerg Zerg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!