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.
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:
After installation, you can use the pm2 command from anywhere in your terminal.
Starting an Application with PM2
To start a Node.js application:
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:
This helps when you have multiple applications on the same server.
Viewing Running Processes
To view all PM2-managed applications:
This shows:
- Process name
- Status
- CPU usage
- Memory usage
- Restart count
Stopping, Restarting, and Deleting Processes
Stop Process
Restart Process
Delete Process
Deleting removes the process from PM2 management.
Viewing Logs
PM2 stores application logs, which are very useful for debugging.
To view logs for a specific application:
Monitoring with PM2
PM2 provides a built-in monitoring dashboard:
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.
In this example:
-i maxtells 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.
Save this as ecosystem.config.js and start it with:
Saving PM2 Process List
To save the currently running applications so they can be restored later:
Auto Start on Server Reboot
PM2 can generate startup scripts so your application restarts automatically when the server reboots.
After running the displayed command, save the process list again:
Reload vs Restart
PM2 supports zero-downtime reloads in cluster mode:
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 saveafter 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.

