Top Related Projects
A simple CLI tool for ensuring that a given script runs continuously (i.e. forever)
Monitor for any changes in your node.js application and automatically restart the server - perfect for development
Package your Node.js project into an executable
🎉 create a single executable out of your node.js apps
Quick Overview
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, reload them without downtime, and facilitate common system admin tasks. PM2 is widely used in the Node.js ecosystem for managing and monitoring applications in production environments.
Pros
- Easy to use with a simple CLI interface
- Built-in load balancer and clustering
- Automatic restart on crashes and system reboots
- Extensive monitoring and logging features
Cons
- Can be overkill for small projects or development environments
- Some advanced features may require a learning curve
- Occasional issues with Windows compatibility
- May add overhead to application startup time
Code Examples
- Starting an application:
const pm2 = require('pm2');
pm2.connect((err) => {
if (err) {
console.error(err);
process.exit(2);
}
pm2.start({
script: 'app.js',
name: 'my-app'
}, (err, apps) => {
pm2.disconnect();
if (err) throw err;
});
});
- Listing running processes:
const pm2 = require('pm2');
pm2.connect((err) => {
if (err) {
console.error(err);
process.exit(2);
}
pm2.list((err, list) => {
console.log(list);
pm2.disconnect();
});
});
- Stopping an application:
const pm2 = require('pm2');
pm2.connect((err) => {
if (err) {
console.error(err);
process.exit(2);
}
pm2.stop('my-app', (err) => {
pm2.disconnect();
if (err) throw err;
});
});
Getting Started
To get started with PM2, follow these steps:
-
Install PM2 globally:
npm install pm2 -g
-
Start an application:
pm2 start app.js --name my-app
-
List running processes:
pm2 list
-
Stop an application:
pm2 stop my-app
-
Restart an application:
pm2 restart my-app
-
Monitor CPU and Memory usage:
pm2 monit
For more advanced usage and configuration options, refer to the official PM2 documentation.
Competitor Comparisons
A simple CLI tool for ensuring that a given script runs continuously (i.e. forever)
Pros of Forever
- Simpler and more lightweight, with fewer dependencies
- Easier to set up and use for basic process management needs
- Better suited for smaller projects or simpler deployment scenarios
Cons of Forever
- Less feature-rich compared to PM2
- Lacks advanced monitoring and clustering capabilities
- Not actively maintained, with fewer updates and community contributions
Code Comparison
Forever:
forever start app.js
forever stop app.js
forever restart app.js
PM2:
pm2 start app.js
pm2 stop app.js
pm2 restart app.js
pm2 monit
pm2 ecosystem
Key Differences
- PM2 offers more advanced features like load balancing, clustering, and monitoring
- Forever is simpler and more straightforward for basic process management
- PM2 has better documentation and a more active community
- Forever is lighter on resources but lacks some of the robustness of PM2
- PM2 provides a web-based dashboard for monitoring, while Forever relies on command-line tools
Both tools serve the purpose of keeping Node.js applications running, but PM2 is generally considered more powerful and feature-rich, while Forever is simpler and easier to use for basic needs.
Monitor for any changes in your node.js application and automatically restart the server - perfect for development
Pros of nodemon
- Lightweight and simple to use, ideal for development environments
- Automatically restarts the application when file changes are detected
- Supports various file types beyond just JavaScript
Cons of nodemon
- Limited production-level features compared to PM2
- Lacks advanced process management and monitoring capabilities
- No built-in load balancing or clustering support
Code Comparison
nodemon:
// package.json
{
"scripts": {
"dev": "nodemon app.js"
}
}
PM2:
// ecosystem.config.js
module.exports = {
apps: [{
name: "app",
script: "app.js",
instances: "max",
exec_mode: "cluster"
}]
}
Key Differences
- nodemon is primarily focused on development, while PM2 is designed for both development and production environments
- PM2 offers more advanced features like process management, monitoring, and load balancing
- nodemon is simpler to set up and use, making it ideal for smaller projects or quick prototyping
- PM2 provides a more robust solution for deploying and managing Node.js applications at scale
Both tools serve different purposes and can be used complementarily in a Node.js project lifecycle, with nodemon for development and PM2 for production deployment and management.
Package your Node.js project into an executable
Pros of pkg
- Creates standalone executables, simplifying distribution
- Supports multiple platforms (Windows, macOS, Linux)
- No runtime dependencies required for packaged applications
Cons of pkg
- Limited to Node.js applications
- Larger file sizes due to bundling Node.js runtime
- May encounter issues with native modules or dynamic requires
Code Comparison
pm2:
const pm2 = require('pm2');
pm2.connect((err) => {
pm2.start({ script: 'app.js', name: 'myapp' }, (err) => {
pm2.disconnect();
});
});
pkg:
// No special code required for pkg
// Simply package your app with:
// pkg app.js
pm2 is a process manager for Node.js applications, focusing on runtime management and monitoring. It excels in production environments, offering features like load balancing, logging, and automatic restarts.
pkg, on the other hand, is a tool for packaging Node.js applications into standalone executables. It's ideal for distributing applications to end-users without requiring Node.js installation.
While pm2 is better suited for server-side deployments and managing long-running processes, pkg is more appropriate for creating distributable applications, especially for desktop or command-line tools.
🎉 create a single executable out of your node.js apps
Pros of nexe
- Creates standalone executable files, simplifying distribution
- No runtime dependencies required on target systems
- Supports cross-platform compilation
Cons of nexe
- Limited to bundling Node.js applications
- May result in larger file sizes compared to source code
- Less suitable for applications requiring frequent updates
Code comparison
nexe:
const nexe = require('nexe');
nexe.compile({
input: './app.js',
output: './app.exe',
targets: ['windows-x64-12.18.2']
}).then(() => console.log('success'));
pm2:
const pm2 = require('pm2');
pm2.connect((err) => {
if (err) throw err;
pm2.start({ script: 'app.js', name: 'myapp' }, (err) => {
if (err) console.error(err);
pm2.disconnect();
});
});
Key differences
- nexe focuses on creating standalone executables, while pm2 is a process manager for Node.js applications
- pm2 offers advanced features like load balancing, monitoring, and log management
- nexe is better suited for distributing applications to end-users, while pm2 is ideal for server-side deployment and management
- pm2 provides more flexibility for updating and scaling applications in production environments
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.
Starting an application in production mode is as easy as:
$ pm2 start app.js
PM2 is constantly assailed by more than 1800 tests.
Official website: https://pm2.keymetrics.io/
Works on Linux (stable) & macOS (stable) & Windows (stable). All Node.js versions are supported starting Node.js 12.X.
Installing PM2
With NPM:
$ npm install pm2 -g
You can install Node.js easily with NVM or FNM.
Start an application
You can start any application (Node.js, Python, Ruby, binaries in $PATH...) like that:
$ pm2 start app.js
Your app is now daemonized, monitored and kept alive forever.
Managing Applications
Once applications are started you can manage them easily:
To list all running applications:
$ pm2 list
Managing apps is straightforward:
$ pm2 stop <app_name|namespace|id|'all'|json_conf>
$ pm2 restart <app_name|namespace|id|'all'|json_conf>
$ pm2 delete <app_name|namespace|id|'all'|json_conf>
To have more details on a specific application:
$ pm2 describe <id|app_name>
To monitor logs, custom metrics, application information:
$ pm2 monit
Cluster Mode: Node.js Load Balancing & Zero Downtime Reload
The Cluster mode is a special mode when starting a Node.js application, it starts multiple processes and load-balance HTTP/TCP/UDP queries between them. This increase overall performance (by a factor of x10 on 16 cores machines) and reliability (faster socket re-balancing in case of unhandled errors).
Starting a Node.js application in cluster mode that will leverage all CPUs available:
$ pm2 start api.js -i <processes>
<processes>
can be 'max'
, -1
(all cpu minus 1) or a specified number of instances to start.
Zero Downtime Reload
Hot Reload allows to update an application without any downtime:
$ pm2 reload all
More informations about how PM2 make clustering easy
Container Support
With the drop-in replacement command for node
, called pm2-runtime
, run your Node.js application in a hardened production environment.
Using it is seamless:
RUN npm install pm2 -g
CMD [ "pm2-runtime", "npm", "--", "start" ]
Read More about the dedicated integration
Host monitoring speedbar
PM2 allows to monitor your host/server vitals with a monitoring speedbar.
To enable host monitoring:
$ pm2 set pm2:sysmonit true
$ pm2 update
Terminal Based Monitoring
Monitor all processes launched straight from the command line:
$ pm2 monit
Log Management
To consult logs just type the command:
$ pm2 logs
Standard, Raw, JSON and formated output are available.
Examples:
$ pm2 logs APP-NAME # Display APP-NAME logs
$ pm2 logs --json # JSON output
$ pm2 logs --format # Formated output
$ pm2 flush # Flush all logs
$ pm2 reloadLogs # Reload all logs
To enable log rotation install the following module
$ pm2 install pm2-logrotate
Startup Scripts Generation
PM2 can generate and configure a Startup Script to keep PM2 and your processes alive at every server restart.
Init Systems Supported: systemd, upstart, launchd, rc.d
# Generate Startup Script
$ pm2 startup
# Freeze your process list across server restart
$ pm2 save
# Remove Startup Script
$ pm2 unstartup
More about Startup Scripts Generation
Updating PM2
# Install latest PM2 version
$ npm install pm2@latest -g
# Save process list, exit old PM2 & restore all processes
$ pm2 update
PM2 updates are seamless
PM2+ Monitoring
If you manage your apps with PM2, PM2+ makes it easy to monitor and manage apps across servers.
Feel free to try it:
Discover the monitoring dashboard for PM2
Thanks in advance and we hope that you like PM2!
CHANGELOG
Contributors
License
PM2 is made available under the terms of the GNU Affero General Public License 3.0 (AGPL 3.0). For other licenses contact us.
Top Related Projects
A simple CLI tool for ensuring that a given script runs continuously (i.e. forever)
Monitor for any changes in your node.js application and automatically restart the server - perfect for development
Package your Node.js project into an executable
🎉 create a single executable out of your node.js apps
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot