PM2 (Node.js)

PM2 is a production process manager for Node.js. It keeps your app running by restarting it if it crashes, runs multiple instances across CPU cores for load balancing, manages logs, and survives server reboots.

Node.js

A bare node server.js process dies if the app crashes or the server restarts, taking your site down. PM2 wraps your app to keep it alive: it automatically restarts on crash, can restart on file change in dev, and with pm2 startup it relaunches your apps after a machine reboot.

Its cluster mode (pm2 start app.js -i max) forks one process per CPU core and load-balances connections across them, letting a single-threaded Node app use all cores. PM2 also aggregates logs (pm2 logs), shows live metrics (pm2 monit), and lists running apps (pm2 list).

An ecosystem.config.js file declares your apps, environments, and instance counts for reproducible deploys. PM2 is one common choice for running Node in production; containers/orchestrators (Docker, Kubernetes) and systemd are alternatives that handle the same restart-and-supervise role.

Syntax

pm2 start app.js -i max --name api

Examples

JavaScript

// run in cluster mode across all cores
// pm2 start server.js -i max --name my-api

// keep it alive across reboots
// pm2 startup
// pm2 save

// view logs and status
// pm2 logs   |   pm2 list

Learn it in a lesson

See also

Back to the Dictionary