Node.js › Deployment & Process Managers (PM2)
How do you configure PM2 using an ecosystem.config.js file rather than passing options directly on the command line?
IntermediateAnswer
An ecosystem file defines one or more applications' configuration (script path, instance count, environment variables per environment, restart behavior, memory limits) in a single version-controlled JavaScript or JSON file, making deployment configuration reproducible, reviewable via source control, and consistent across environments rather than relying on manually remembered command-line flags.
// ecosystem.config.js
module.exports = {
apps: [{
name: 'my-app',
script: './server.js',
instances: 'max',
exec_mode: 'cluster',
env_production: { NODE_ENV: 'production', PORT: 3000 },
max_memory_restart: '500M',
}],
};
// pm2 start ecosystem.config.js --env productionReal-world example A team checks their ecosystem.config.js into version control alongside their application code, ensuring the exact same process configuration (instance count, memory limits, environment variables) is used identically whenever any team member or CI pipeline deploys the application, rather than relying on someone remembering the correct command-line flags.