Find us on social media
PM2Node.jsDeploy6 min read
Deploy with PM2 (Node.js)
Deploy Node.js applications in production with PM2 for process management and automatic restarts.
PM2 is a production process manager for Node.js applications. It handles restarts, clustering, logging and startup scripts.
Step 1 — Install PM2
bash
npm install -g pm2Step 2 — Start your application
bash
pm2 start app.js --name "my-app"For ES modules or TypeScript:
bash
pm2 start server.mjs --name "my-api"
pm2 start dist/index.js --name "my-ts-app"Step 3 — Useful PM2 commands
bash
pm2 list # List all processes
pm2 logs my-app # View logs
pm2 restart my-app # Restart
pm2 stop my-app # Stop
pm2 delete my-app # Remove from PM2
pm2 monit # Real-time monitoringStep 4 — Ecosystem file
Create ecosystem.config.js for complex setups:
javascript
module.exports = {
apps: [{
name: 'my-api',
script: './dist/index.js',
instances: 'max',
exec_mode: 'cluster',
env: {
NODE_ENV: 'production',
PORT: 3000
},
max_memory_restart: '500M',
log_date_format: 'YYYY-MM-DD HH:mm:ss'
}]
};Start with:
bash
pm2 start ecosystem.config.jsStep 5 — Auto-start on boot
bash
pm2 startupRun the command it outputs (with sudo), then save the current process list:
bash
pm2 saveStep 6 — Zero-downtime reload
For cluster mode applications:
bash
pm2 reload my-apiStep 7 — Log management
bash
pm2 install pm2-logrotate
pm2 set pm2-logrotate:max_size 10M
pm2 set pm2-logrotate:retain 7Deployment workflow
bash
git pull origin main
npm install --production
npm run build
pm2 reload my-apiTip
Combine PM2 with Nginx as a reverse proxy for production deployments on your Baires Host VPS. PM2 handles the Node.js process while Nginx handles SSL, static files and load balancing.
Was this guide helpful?