Search code examples
node.jsexpresspm2

pm2 cannot start express application


I wrote a simple Express application. It ran correctly with "npm start". The scripts in package.json is this:

"type": "module",
"scripts": {
  "start": "src/bin/www.js"
}

But I couldn't execute it like this:

pm2 start ./src/bin/www.js

I checked pm2.log. It shown "had too many unstable restarts(16)".

Then ran it with pm2-dev:

pm2-dev start ./src/bin/www.js

Reported the error:

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

Pm2 can't work well with import/export? The node version is 14.16.0.


Solution

  • First make sure if you have following two points:

    1. Node version >= 14. It only works with latest version of node.
    2. Make sure your package.json includes a line for "type": "module", Without this line node assumes you want to use Common JS modules rather than ESM.

    If both are true and it's still not working then you might be victim of following github issue

    https://github.com/Unitech/pm2/issues/4540

    For now you can try running it with babel!