pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

[ERR_REQUIRE_ESM]: Must use import to load module

Open kostadinSpiridonov opened this issue 5 years ago • 5 comments

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

kostadinSpiridonov avatar Aug 06 '20 14:08 kostadinSpiridonov

I have the same issue. It is super weird. how we can fix it?

basir avatar Sep 07 '20 20:09 basir

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"

genru avatar Feb 27 '22 02:02 genru

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"
      }]
  }

jpolvora avatar Mar 30 '22 14:03 jpolvora

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'
    }]
}

luffs avatar May 15 '22 17:05 luffs