Chat on WhatsApp
CR Guard
CloudRevol
← Back to Blog

PM2 Complete Guide for Node.js Applications

PM2 Complete Guide for Node.js Applications

Learn how to install PM2, manage Node.js applications, configure startup scripts, use ecosystem files, enable cluster mode and monitor production workloads.

PM2Node.jsCluster ModeEcosystem FileDevOps

Install PM2

npm install -g pm2

Verify Installation

pm2 --version

Start Application

pm2 start app.js
pm2 start server.js

Start Application With Name

pm2 start app.js --name backend-api

View Running Processes

pm2 list

Restart Application

pm2 restart backend-api

Stop Application

pm2 stop backend-api

Delete Application

pm2 delete backend-api

Monitor Logs

pm2 logs
pm2 logs backend-api

Real-Time Monitoring

pm2 monit

Enable Startup On Server Reboot

pm2 startup
pm2 save

This ensures applications automatically start after reboot.

Ecosystem File Example-1

module.exports = {
  apps: [
    {
      name: "backend-api",
      script: "./server.js",
      instances: 2,
      exec_mode: "cluster",
      watch: false,
      autorestart: true,
      max_memory_restart: "500M",
      env: {
        NODE_ENV: "production",
        PORT: 3000
      }
    }
  ]
};

Start Using Ecosystem File

pm2 start ecosystem.config.js

Reload Zero Downtime

pm2 reload ecosystem.config.js

Fork Mode Example

Fork mode runs a single instance of your application. This is ideal for development environments, small websites, internal tools, or applications with low traffic.

pm2 start server.js --name backend-api

When To Use Fork Mode

  • ✓ Development environments
  • ✓ Low traffic websites
  • ✓ Background workers
  • ✓ Cron jobs
  • ✓ Small APIs

Cluster Mode Example

Cluster mode allows PM2 to use multiple CPU cores. This improves performance, scalability, and application availability for production workloads.

pm2 start server.js -i max --name backend-api

The max option automatically uses all available CPU cores.

When To Use Cluster Mode

  • ✓ Production APIs
  • ✓ High traffic websites
  • ✓ SaaS applications
  • ✓ Node.js microservices
  • ✓ Load-balanced applications

Useful PM2 Commands

Show Process Details
pm2 show backend-api
Flush Logs
pm2 flush
Restart All Apps
pm2 restart all
Stop All Apps
pm2 stop all
Update PM2
npm install pm2@latest -g && pm2 update
Scale Up Instances
pm2 scale backend-api 8

PM2 Best Practices

Follow these production-ready PM2 recommendations to improve reliability, performance, monitoring, and scalability of your Node.js applications.

🚀

Use Cluster Mode

Utilize all CPU cores by running multiple instances for improved scalability and performance.

🔄

Enable Auto Restart

Automatically recover applications after crashes without manual intervention.

📄

Use Ecosystem Files

Store all application configurations in a central ecosystem.config.js file.

🧠

Set Memory Limits

Prevent runaway processes by configuring max_memory_restart values.

📊

Monitor Regularly

Use pm2 monit and logs to proactively identify performance bottlenecks.

🔐

Protect Secrets

Store API keys, database credentials, and tokens in environment variables.

⚡

Zero Downtime Deployments

Use PM2 reload commands to deploy updates without service interruptions.

💾

Enable Startup Scripts

Configure PM2 startup and save commands to survive server reboots.

🛡️

Keep PM2 Updated

Always run the latest stable PM2 version for security fixes and improvements.

Frequently Asked Questions

What is PM2 used for?

PM2 is a production process manager for Node.js applications. It helps manage processes, restart crashed applications, monitor resource usage, enable clustering, and automate startup after server reboots.

Is PM2 free to use?

Yes. PM2 Open Source is free and widely used by developers and businesses to run Node.js applications in production.

Should I use PM2 in production?

Absolutely. PM2 provides process monitoring, automatic restarts, clustering, memory management, logging and zero-downtime reloads, making it one of the most popular Node.js production tools.

What is the difference between PM2 fork mode and cluster mode?

Fork mode runs a single application instance, while cluster mode runs multiple instances across available CPU cores, improving scalability and performance.

How do I start a Node.js application automatically after reboot?

Use PM2 startup followed by PM2 save. This creates startup services so your applications automatically restart when the server reboots.

Does PM2 support zero-downtime deployments?

Yes. PM2 reload commands allow applications running in cluster mode to reload without interrupting active users.

How many PM2 instances should I run?

Most production environments use one PM2 instance per CPU core. The command "pm2 start app.js -i max" automatically uses all available CPU cores.

Is PM2 better than Node.js directly?

PM2 adds process management, monitoring, automatic recovery, logging, clustering and deployment features that are not available when running Node.js directly.

Can PM2 manage multiple applications?

Yes. PM2 can manage multiple Node.js applications, microservices, workers, cron jobs and APIs from a single server.

What are the best PM2 production practices?

Use cluster mode, enable startup scripts, configure memory limits, use ecosystem files, monitor logs regularly, and perform zero-downtime deployments whenever possible.