最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PM2 with ES module. Error: ERR_REQUIRE_ESM - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Apr 20, 2023 at 10:54 Sha'an asked Nov 25, 2021 at 17:46 Sha'anSha'an 1,2761 gold badge13 silver badges26 bronze badges 3
  • What does your importing module syntax look like? – Randy Casburn Commented Nov 25, 2021 at 18:05
  • 1 It's old style importing there require(). It seems PM2 doesn't support ES Modules yet. But I find a temporary solution for this, here: github.com/nodejs/modules/issues/293 – Sha'an Commented Nov 25, 2021 at 20:09
  • 1 You will have very limited success, if any, mixing and matching module formats. You should stick with CommonJS. – Randy Casburn Commented Nov 25, 2021 at 22:32
Add a comment  | 

2 Answers 2

Reset to default 16

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

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

  }]
}
发布评论

评论列表(0)

  1. 暂无评论