How to Install and Configure PM2 for Node.js Apps
A step‑by‑step guide to keeping your Node.js services alive and production‑ready with PM2.
PM2 is a powerful process manager for Node.js applications. It ensures your apps stay online, restarts them automatically if they crash, and provides monitoring tools for performance and logs. This guide walks through installing PM2, starting a Node.js service, configuring startup scripts, and enabling monitoring so your apps are production‑ready.
Photo by James Wiseman on Unsplash
Step 1: Install PM2
Install PM2 globally using npm:
npm install -g pm2
This makes the pm2 command available system‑wide.
Step 2: Start Your Node.js App
Navigate to your project folder and start your app with PM2:
cd /path/to/your/app
pm2 start app.js
PM2 will keep app.js running in the background.
Step 3: Save and Auto‑Start on Boot
To ensure PM2 restarts your apps after a server reboot:
pm2 save
pm2 startup
This generates startup scripts for your operating system and saves the current process list.
Step 4: Monitor and Manage
PM2 provides built‑in monitoring tools:
pm2 list # show running apps
pm2 logs app # view logs
pm2 monit # live monitoring dashboard
These commands help you track uptime, CPU usage, and memory consumption.
Benefits of Using PM2
- Automatic restarts if your app crashes.
- Easy process management with simple commands.
- Startup scripts for production environments.
- Monitoring and logging built in.
Conclusion
PM2 is an essential tool for running Node.js apps in production. With installation, startup configuration, and monitoring in place, your services will stay resilient and easier to manage.