Imagine your Node.js app is like a robot 🤖 running on your computer.
What if it suddenly crashes? Or uses too much memory?
That’s where PM2 comes in — your app’s personal doctor 🩺 and bodyguard 🛡️!
🔍 What is PM2?
PM2 is a process manager for Node.js.
It helps you:
- Keep your app always running (even if it crashes)
- Restart it automatically when needed
- Monitor memory and CPU usage
- View logs easily
- Handle multiple apps
🧠 Think of PM2 like a smart babysitter for your app.
🛠️ How to Install PM2
First, open your terminal and run:
bashCopyEditnpm install -g pm2
The
-g
flag installs PM2 globally so you can use it anywhere on your machine 🌎.
🚦 Starting Your App with PM2
Instead of using node app.js
, use:
bashCopyEditpm2 start app.js
✅ Your app is now being watched by PM2 like a hawk 🦅.
🔄 Auto-Restart on Crashes
PM2 will auto-restart your app if it crashes. No manual fix needed 🙌.
Example:
bashCopyEditpm2 start server.js --watch
🧠 The --watch
flag tells PM2 to restart your app if any file changes — super helpful for development!
📊 Monitor Your App’s Health
To see live performance:
bashCopyEditpm2 monit
This opens a dashboard like this (image placeholder):

You’ll see:
- CPU Usage
- Memory Usage
- Restart counts
👀 It’s like looking at your app’s heartbeat ❤️.
📝 Viewing Logs with PM2
Logs are like your app’s diary 📖.
To see logs:
bashCopyEditpm2 logs
You’ll see:
- Console outputs
- Errors
- Warnings
🔧 This is super helpful for debugging!
♻️ Log Rotation (So It Doesn’t Fill Your Disk!)
Logs can grow BIG. You don’t want your computer to explode 💣.
PM2 has a feature called log rotation.
Install the log rotate module:
bashCopyEditpm2 install pm2-logrotate
You can also configure it:
bashCopyEditpm2 set pm2-logrotate:max_size 10M
pm2 set pm2-logrotate:retain 5
✅ This keeps your logs clean and small.
🔄 Auto Start on Reboot
Want your app to start automatically when your server reboots?
Just run:
bashCopyEditpm2 startup
Then follow the instructions PM2 gives you.
Save your running apps:
bashCopyEditpm2 save
💡 Now even after a power cut, your app is back up like magic 🔌✨!
🧼 Stopping & Managing Apps
Some useful PM2 commands:
Command | What it does |
---|---|
pm2 list | Shows all running apps |
pm2 stop app_name | Stops an app |
pm2 restart app_name | Restarts an app |
pm2 delete app_name | Removes app from PM2 |
💬 Example:
bashCopyEditpm2 stop app.js
📦 Deploying with PM2 (Bonus)
You can even use PM2 to deploy code to remote servers.
It supports ecosystem files and zero-downtime reloads.
But that’s a topic for another guide! 😉 Stay tuned.
🧠 Why Use PM2?
✅ Keeps your app running
✅ Easy logging
✅ Auto-restart on crash
✅ Health monitoring
✅ Works great in production
Perfect tool for developers — even beginners!
🧾 Summary
Feature | Benefit |
---|---|
Auto-Restart | Keeps app alive always |
Logs | See errors, output |
Monitoring | View live CPU/Memory stats |
Log Rotation | Saves disk space |
Startup Script | Runs app after reboot |
📌 Final Tips
- Always use
pm2 save
after starting apps - Use
pm2 logs
to debug issues - Monitor your app regularly with
pm2 monit
📣 Like This Tutorial? Follow Web Codder 👇
If you enjoyed this friendly tech guide, you’ll love our video tutorials and coding tips!
👉 Subscribe on YouTube: Web Codder
📸 Follow on Instagram: @web_codder_official
💬 Join our WhatsApp Community: webcodder.dev/whatsapp
Let’s learn and build cool stuff together! 💻🔥