pm2 with ecosystem.config.js fails
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 43.3k
- Forks
- 2.7k
- PR merge metrics
- No merged PRs in 30d
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing ecosystem.config.js loading in lib/Common.js, then follow the calls through lib/API.js and the CLI entry point shown in the stack trace. Reproduce with Node v17.8.0, PM2 5.2.0, and a package.json declaring type: module; done means the documented ecosystem.config.js scenario no longer fails with ERR_REQUIRE_ESM.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100