🟢 Node.js Intermediate

What is PM2 and why is it used in production?

Why Interviewers Ask This

Mid-level Node.js roles require deep understanding of this topic. Interviewers ask this to separate candidates who truly understand the mechanics from those who only know surface-level concepts.

Answer

PM2 is a production process manager for Node.js applications. It handles critical production concerns that you shouldn't manage manually. Key features: (1) Process management: start, stop, restart, reload applications — pm2 start app.js --name myapp; (2) Auto-restart: automatically restarts the app if it crashes; (3) Cluster mode: spawns one worker per CPU core with zero-downtime reload — pm2 start app.js -i max; (4) Load balancing: distributes requests across workers; (5) Log management: aggregates logs from all workers — pm2 logs; (6) Monitoring: CPU and memory usage — pm2 monit; (7) Startup scripts: pm2 startup generates a systemd/init script so PM2 restarts after server reboots; (8) Ecosystem files: ecosystem.config.js for configuration as code. In containerized environments (Docker/Kubernetes), PM2 is less necessary since the orchestrator handles restarts and scaling, but it's still useful in bare-metal/VM deployments.

Common Mistake

Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex Node.js answers easy to follow.