← Back to Node.js Course | Chapter 10: Deployment & Best Practices | Lesson 3 of 7

PM2 basics

PM2 keeps your Node app running, restarts it if it crashes and can run several copies.

In this page:

  1. PM2 basics
Syntax
javascript
pm2 start app.js
pm2 list
pm2 restart app
pm2 stop app

PM2 basics

PM2 is a process manager. pm2 start app.js runs the app in the background, pm2 list shows status, pm2 logs streams output and pm2 restart applies changes. Cluster mode runs one process per CPU core and an ecosystem file records settings.

Note: Use pm2 startup and pm2 save to restart apps after a reboot.

Example: PM2 basics

bash
$ npm install -g pm2
$ pm2 start app.js -i max --name api
$ pm2 list
$ pm2 logs api
$ pm2 save && pm2 startup

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
Common Mistakes
  1. Running node directly on a server
  2. Not saving the process list
  3. Ignoring log rotation
Chapter Summary
  • PM2 restarts crashed apps
  • start, list, logs, restart
  • Cluster mode uses all cores
  • Ecosystem files store config
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.