Using PM2

Node js 8 min min read Updated: Mar 30, 2026 Advanced
Using PM2
Advanced Topic 2 of 8

Using PM2 in Node.js

When a Node.js application is deployed to production, simply running it with node app.js is usually not enough. Production applications need process management, automatic restarts, monitoring, logging, and better uptime handling. This is where PM2 becomes very useful.

PM2 is one of the most popular process managers for Node.js applications. It helps you run, monitor, restart, and scale your applications easily in production environments.

Key Concept: PM2 is a production process manager for Node.js that keeps applications running, restarts them on failure, and helps manage scaling and monitoring.

What is PM2?

PM2 is a process manager for Node.js applications. It is designed to simplify application management in production by providing features such as:

  • Automatic restart on crashes
  • Process monitoring
  • Log management
  • Cluster mode for scaling across CPU cores
  • Startup scripts for server reboot persistence

In simple words, PM2 makes sure your application stays alive and manageable after deployment.

Why Use PM2?

  • High availability: Restarts app automatically if it crashes
  • Easy monitoring: View CPU, memory, and process status
  • Log management: Centralized logs for debugging
  • Cluster mode: Use multiple CPU cores
  • Startup on reboot: Automatically start the app after server restart

Installing PM2

PM2 is usually installed globally:

bash npm install -g pm2

After installation, you can use the pm2 command from anywhere in your terminal.

Starting an Application with PM2

To start a Node.js application:

bash pm2 start app.js

This starts the application under PM2 management instead of running it directly with Node.js.

Starting an App with a Custom Name

You can assign a custom process name for easier management:

bash pm2 start app.js --name my-api

This helps when you have multiple applications on the same server.

Viewing Running Processes

To view all PM2-managed applications:

bash pm2 list

This shows:

  • Process name
  • Status
  • CPU usage
  • Memory usage
  • Restart count

Stopping, Restarting, and Deleting Processes

Stop Process

bash pm2 stop my-api

Restart Process

bash pm2 restart my-api

Delete Process

bash pm2 delete my-api

Deleting removes the process from PM2 management.

Viewing Logs

PM2 stores application logs, which are very useful for debugging.

bash pm2 logs

To view logs for a specific application:

bash pm2 logs my-api

Monitoring with PM2

PM2 provides a built-in monitoring dashboard:

bash pm2 monit

This shows real-time CPU and memory usage for your applications.

Using PM2 in Cluster Mode

PM2 can automatically run your application in cluster mode to use all CPU cores.

bash pm2 start app.js -i max

In this example:

  • -i max tells PM2 to create as many instances as there are CPU cores

This is useful for high-traffic applications that need better performance.

Using an Ecosystem File

PM2 supports configuration through an ecosystem file, which is useful for managing multiple settings in one place.

javascript module.exports = { apps: [ { name: "my-api", script: "app.js", instances: "max", exec_mode: "cluster", env: { NODE_ENV: "production", PORT: 3000 } } ] };

Save this as ecosystem.config.js and start it with:

bash pm2 start ecosystem.config.js

Saving PM2 Process List

To save the currently running applications so they can be restored later:

bash pm2 save

Auto Start on Server Reboot

PM2 can generate startup scripts so your application restarts automatically when the server reboots.

bash pm2 startup

After running the displayed command, save the process list again:

bash pm2 save

Reload vs Restart

PM2 supports zero-downtime reloads in cluster mode:

bash pm2 reload my-api

This is better than restart in some production scenarios because it helps avoid downtime.

Why PM2 is Useful in Production

In production, servers can face crashes, memory spikes, or unexpected failures. PM2 reduces operational risk by automatically restarting apps and providing better process visibility.

It also makes deployment easier because you can manage applications with simple commands.

Best Practices for Using PM2

  • Use custom names for processes
  • Use ecosystem files for structured configuration
  • Run in cluster mode for scalable APIs
  • Check logs regularly for errors
  • Save process list after configuration changes
  • Use startup scripts in production environments

Common Mistakes

  • Forgetting to run pm2 save after setup
  • Using restart instead of reload when zero downtime is needed
  • Not monitoring memory and CPU usage
  • Running too many instances without checking system resources
  • Ignoring log files and crash loops

PM2 vs Running with node

Feature node app.js PM2
Crash recovery No Yes
Monitoring No Yes
Cluster mode Manual setup Built-in
Log management Basic Advanced
Startup on reboot No Yes

Real-World Use Cases

  • Running Express.js APIs in production
  • Managing background workers
  • Keeping chat or socket servers alive
  • Running cluster-mode applications across CPU cores

Conclusion

PM2 is an essential tool for running Node.js applications in production. It gives you process stability, automatic restarts, monitoring, logging, and scaling support in a simple way.

If you are deploying Node.js APIs or real-time applications, learning PM2 is highly recommended. It is one of the easiest ways to improve uptime and process management in production environments.

Quick Summary: PM2 is a process manager for Node.js that helps run applications reliably in production with restart, monitoring, logging, and scaling features.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators