Unitech / pm2

Node.js Production Process Manager with a built-in Load Balancer.
https://pm2.keymetrics.io/docs/usage/quick-start/
Other
41.62k stars 2.63k forks source link

pm2 with ecosystem.config.js fails #5337

Open miracuruzrh opened 2 years ago

miracuruzrh commented 2 years ago

What's going wrong?

pm2 with configuration filename= ecosystem.config.js fails. Error log is shown below. When I rename the filename from ecosystem.config.js to ecosystem.config_a.js then the the app starts without error. My app which i start with pm2 (index.js) is written in ES6. If someone could look into this it would be much apprechiated. Please let me know if additional data is required.

content of ecosystem.config.js (sensitive data replaced with "x"):

module.exports = {
    apps: [{
      name: 'SolarTrackerAPI',
      script: './src/index.js',
      watch: '.',
      env: {
        PORT: '3000',
        NODE_ENV: 'development',
        SQL_HOST: '192.168.1.20',
        SQL_USER: 'xxx',
        SQL_PASSWORD: 'xxxx',
        SQL_DB: 'ttn',
        SQL_TABLE: 'tbl_ttn',
        TTN_TOPIC: '#',
        TTN_CLIENTID: 'MyMQTT',
        TTN_PORT: '1883',
        TTN_BROKER_URL: 'mqtt://eu1.cloud.thethings.network',
        TTN_USERNAME: 'xxxxxx@ttn',
        TTN_TOKEN: 'NNSXS.xxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxx'
      }
    }]
  }

Error log:

 pm2 start ./config/ecosystem.config.js 
[PM2][ERROR] File ./config/ecosystem.config.js malformated
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/simon/Documents/GitLab/Solartracker Projekt/Webseite/Server/config/ecosystem.config.js from /usr/local/lib/node_modules/pm2/lib/Common.js not supported.
ecosystem.config.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename ecosystem.config.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/simon/Documents/GitLab/Solartracker Projekt/Webseite/Server/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at Object.Common.parseConfig (/usr/local/lib/node_modules/pm2/lib/Common.js:317:12)
    at API._startJson (/usr/local/lib/node_modules/pm2/lib/API.js:934:25)
    at API.start (/usr/local/lib/node_modules/pm2/lib/API.js:329:12)
    at /usr/local/lib/node_modules/pm2/lib/binaries/CLI.js:290:13
    at /usr/local/lib/node_modules/pm2/node_modules/async/internal/withoutIndex.js:8:40
    at replenish (/usr/local/lib/node_modules/pm2/node_modules/async/internal/eachOfLimit.js:81:17)
    at /usr/local/lib/node_modules/pm2/node_modules/async/internal/eachOfLimit.js:86:9
    at eachLimit (/usr/local/lib/node_modules/pm2/node_modules/async/forEachLimit.js:47:43)
    at awaitable (/usr/local/lib/node_modules/pm2/node_modules/async/internal/awaitify.js:13:28)
    at Command.<anonymous> (/usr/local/lib/node_modules/pm2/lib/binaries/CLI.js:289:7)
    at Command.listener (/usr/local/lib/node_modules/pm2/node_modules/commander/index.js:315:8)
    at Command.emit (node:events:527:28)
    at Command.parseArgs (/usr/local/lib/node_modules/pm2/node_modules/commander/index.js:651:12)
    at Command.parse (/usr/local/lib/node_modules/pm2/node_modules/commander/index.js:474:21)
    at beginCommandProcessing (/usr/local/lib/node_modules/pm2/lib/binaries/CLI.js:147:13)
    at /usr/local/lib/node_modules/pm2/lib/binaries/CLI.js:221:7
    at /usr/local/lib/node_modules/pm2/lib/API.js:181:16
    at /usr/local/lib/node_modules/pm2/lib/Client.js:54:16
    at Timeout._onTimeout (/usr/local/lib/node_modules/pm2/lib/Client.js:372:9) {
  code: 'ERR_REQUIRE_ESM'
}

How could we reproduce this issue?

Create an application with ES6 use filename "ecosystem.config.js". Then the error should occur

Supporting information

Versions

pm2 --version 5.2.0 node --version v17.8.0 npm --version 8.5.5

miracuruzrh commented 2 years ago

Update:

I am sorry. I have just recognized that renaming did not worked. Wit the command: pm2 start ./config/ecosystem.config_a.js simply created a job with the name "ecosystem.config_a" was started. It did not took the config file as configuration input.

If someone could help me, how to bring pm2 together with ES6 to work that would be nice. I have found already a solution that the extension .cjs should be used. But I don't know if this is the correct way.

ralyodio commented 2 years ago

https://stackoverflow.com/questions/71825361/pm2-ecosystem-file-pm2error-file-ecosystem-config-js-malformated

karts-with-freaking-lasers commented 2 years ago

I encountered this same issue yesterday.

Our software structure:

/path/to/projects
│    ecosystem.config.js (app: "Project 1", e.g.,)
|
└> Project 1
   │   ecosystem.config.js (newly added for storage in repository)
   |   package.json => { "type" : "module" }

Project 1 is a Typescript ESM module which uses tsc-fix-esm so we can run node Project1/build/index.js successfully.

We tried a thing where we added the ecosystem.config.js to a project so we have historical references via Bitbucket.

This PM2 error came as a result of the project configuration (ESM) and the ecosystem.config.js does not align with that.

Moving ecosystem.config.js up a directory from Project 1 resolved this issue for us.

I would venture a guess that renaming the file to ecosystem.config.cjs would have worked as well.

Hope this helps anyone else encountering this seemingly-random issue.

steve2507 commented 2 years ago

@karts-with-freaking-lasers can confirm that renaming to ecosystem.config.cjs solves the issue.

Firsh commented 2 years ago

Is the ecosystem configuration file supposed to hold multiple unrelated projects/services? As in a single global config, sitting in the home folder? If so how do you version control that if it's up one level of each individual project?

I initially thought it was just a per project file in order to save us from writing long command line parameters. It could include multiple services but realistically those that are tied together by the same project (like backend and frontend or similar).

mostafa-gado commented 2 years ago

Hi I have a similar error but not sure if it is the same. I created the ecosystem file and added two tokens. The error is pointing to the token value and saying unexpected string. File content: module.exports = { apps : [{ name: 'etit-erp-backend', script: "etit-erp-backend/express-server.js", env: { NODE_ENV: "production", ACCESS_TOKEN_SECRET: "<token>" REFRESH_TOKEN_SECRET: "<token>" } }] } error stack: `$ pm2 start ecosystem.config.js [PM2][ERROR] File ecosystem.config.js malformated /home/opc/ecosystem.config.js:8 REFRESH_TOKEN_SECRET: "8bcba733fb2326ce8135......." ^^^^^^^^^^^^^^^^^^^^

SyntaxError: Unexpected identifier at Object.compileFunction (node:vm:352:18) at wrapSafe (node:internal/modules/cjs/loader:1032:15) at Module._compile (node:internal/modules/cjs/loader:1067:27) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Module.require (node:internal/modules/cjs/loader:1005:19) at require (node:internal/modules/cjs/helpers:102:18) at Object.Common.parseConfig (/usr/local/lib/node_modules/pm2/lib/Common.js:317:12) at API._startJson (/usr/local/lib/node_modules/pm2/lib/API.js:934:25) `

deivan commented 1 year ago

Hello,

for me was useful just rename ecosystem.config.js to ecosystem.config.cjs

koistya commented 1 year ago

It seems like pm2 deploy command cannot locate ecosystem.config.cjs though.

hendisantika commented 1 year ago

I have same issue here:

[PM2][ERROR] File ecosystem.config.js malformated
Error [ERR_REQUIRE_ESM]: require() of ES Module /var/www/html/D-ERP-UI/ecosystem.config.js from /usr/local/lib/node_modules/pm2/lib/Common.js not supported.
ecosystem.config.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename ecosystem.config.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /var/www/html/D-ERP-UI/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at Common.parseConfig (/usr/local/lib/node_modules/pm2/lib/Common.js:317:12)
    at API._startJson (/usr/local/lib/node_modules/pm2/lib/API.js:934:25)
    at API.start (/usr/local/lib/node_modules/pm2/lib/API.js:329:12)
    at /usr/local/lib/node_modules/pm2/lib/binaries/CLI.js:290:13
    at /usr/local/lib/node_modules/pm2/node_modules/async/internal/withoutIndex.js:8:40
    at replenish (/usr/local/lib/node_modules/pm2/node_modules/async/internal/eachOfLimit.js:81:17)
    at /usr/local/lib/node_modules/pm2/node_modules/async/internal/eachOfLimit.js:86:9
    at eachLimit (/usr/local/lib/node_modules/pm2/node_modules/async/forEachLimit.js:47:45)
    at awaitable (/usr/local/lib/node_modules/pm2/node_modules/async/internal/awaitify.js:14:28)
    at Command.<anonymous> (/usr/local/lib/node_modules/pm2/lib/binaries/CLI.js:289:7)
    at Command.listener (/usr/local/lib/node_modules/pm2/node_modules/commander/index.js:315:8)
    at Command.emit (node:events:514:28)
    at Command.parseArgs (/usr/local/lib/node_modules/pm2/node_modules/commander/index.js:651:12)
    at Command.parse (/usr/local/lib/node_modules/pm2/node_modules/commander/index.js:474:21)
    at beginCommandProcessing (/usr/local/lib/node_modules/pm2/lib/binaries/CLI.js:147:13)
    at /usr/local/lib/node_modules/pm2/lib/binaries/CLI.js:221:7
    at /usr/local/lib/node_modules/pm2/lib/API.js:181:16
    at /usr/local/lib/node_modules/pm2/lib/Client.js:54:16
    at Timeout._onTimeout (/usr/local/lib/node_modules/pm2/lib/Client.js:372:9) {
  code: 'ERR_REQUIRE_ESM'
}

pm2 --version 5.2.0 node --version v20.9.0 npm --version 10.1.0 pnpm --version 8.10.2 yarn --version 1.22.19

NPCBOSS commented 1 year ago

It seems like pm2 deploy command cannot locate ecosystem.config.cjs though.

I try this, it is ok