Production Configuration, Environment Variables & PM2 Process Manager0%

Production Configuration, Environment Variables & PM2 Process Manager

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Production Configuration, Environment Variables & PM2 Process Manager

Running node server.js in a terminal is fine for local testing, but deploying a raw command in production means the moment an uncaught exception occurs or the server machine restarts, your entire website goes offline. Professional Node.js production environments require environment configuration management, graceful shutdown handling, and a process manager like PM2.


1. Managing Environment Variables (dotenv)

Never hardcode database connection strings, API secrets, or ports in code:

Bash / Terminal
# .env (NEVER commit to Git! Add to .gitignore)
PORT=5000
NODE_ENV=production
DATABASE_URL=mongodb+srv://admin:secret@cluster0.mongodb.net/prod_db
JWT_SECRET=c8d48291a9b24479e0f2b380d6b99
CORS_ORIGIN=https://mskinstitute.com

Load environment variables at the absolute entry point of your server:

JavaScript
import 'dotenv/config';
 
const port = process.env.PORT || 5000;
console.log(`Running in ${process.env.NODE_ENV} mode on port ${port}`);

2. Why PM2 (Process Manager 2)?

PM2 is the production runtime process manager for Node.js:

  • Zero-Downtime Reloads: Reloads updated code without dropping a single active client connection.
  • Cluster Mode: Automatically spawns worker processes across all available CPU cores, bypassing the single-threaded limit!
  • Automatic Restarts: Instantly revives dead worker instances upon crashes or memory leaks.
  • System Startup Hook: Configures systemd/init scripts so the application reboots automatically if the server machine restarts.

3. Configuring PM2 with ecosystem.config.cjs

JavaScript
module.exports = {
apps: [
{
name: 'msk-backend-api',
script: './src/server.js',
instances: 'max', // Spawns 1 process per CPU core!
exec_mode: 'cluster',
autorestart: true,
watch: false, // Do NOT watch files in production
max_memory_restart: '1G', // Restart worker if memory exceeds 1 GB
env_production: {
NODE_ENV: 'production',
PORT: 5000
}
}
]
};

4. Essential PM2 CLI Commands

Bash / Terminal
# Start application in cluster mode using configuration
pm2 start ecosystem.config.cjs --env production
 
# View real-time cluster status and memory usage
pm2 status
 
# Real-time streaming log viewer
pm2 logs msk-backend-api
 
# Zero-downtime rolling code reload
pm2 reload msk-backend-api
 
# Generate OS system startup script
pm2 startup
pm2 save

Interactive Quiz

Multiple Choice Questions

Question 1
What is the primary benefit of running PM2 in cluster mode with instances: 'max'?
Question 2
Why should the .env file containing secrets never be committed to a public or team Git repository?
Question 3
What command configures PM2 to automatically revive all registered applications upon server machine reboot?
Question 4
What is the advantage of pm2 reload over pm2 restart during a production deployment?
Question 5
What does the max_memory_restart: '1G' setting in PM2 do?

Related Lessons

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext