What is PM2 and when would you use it?

IntermediateConceptNode.js builds (npm, package.json)
Answer

Short answer

PM2 is a process manager for Node.js on plain servers/VMs. It keeps the app running (restarts it if it crashes), runs multiple instances to use all CPU cores (cluster mode), manages logs, and restarts apps after a server reboot.

Example

npm install -g pm2
pm2 start dist/server.js --name shop-api -i max   # one process per CPU core
pm2 list
pm2 logs shop-api
pm2 reload shop-api                               # zero-downtime reload
pm2 save && pm2 startup                           # survive reboots

ecosystem.config.js keeps the settings in a file:

module.exports = { apps: [{ name: "shop-api", script: "dist/server.js", instances: "max", env: { NODE_ENV: "production" } }] };

Why Node needs this

A Node process runs JavaScript on one CPU core. On a 4-core VM, a single process wastes 75% of the CPU — PM2 cluster mode runs 4.

In containers?

Usually not needed: Kubernetes restarts crashed containers and you scale with more pods. Run node server.js directly as PID 1 (or with tini/--init to handle signals).

What to say in the interview

"PM2 is for VMs: process supervision, cluster mode across cores, log management and startup on boot. In Kubernetes we don't use it — the orchestrator handles restarts and scaling, and we run one Node process per container."

All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Node.js apps: npm & package.json

More on Node.js builds (npm, package.json)