Passing Environment Variables to Node.js with PM2: A Comprehensive Guide
Problem: You need to manage your Node.js application's configuration dynamically based on different environments (e.g., development, testing, production). Hardcoding values within the application code isn't ideal, as it requires manual changes for every environment switch.
Solution: Environment variables provide a flexible and robust method to manage your application's configuration. PM2, a popular process manager for Node.js, offers a convenient way to set and pass environment variables to your running Node.js applications.
Understanding the Scenario
Imagine you're building a Node.js application that requires a database connection. Hardcoding the database credentials directly into your code would lead to security risks and make managing different environments cumbersome. Instead, you can leverage environment variables to dynamically set these values.
Here's a simplified example of a Node.js application:
// app.js
const express = require('express');
const app = express();
const dbHost = process.env.DB_HOST || 'localhost';
const dbUser = process.env.DB_USER || 'your_user';
const dbPassword = process.env.DB_PASSWORD || 'your_password';
const dbDatabase = process.env.DB_DATABASE || 'your_database';
// Connect to the database using environment variables
// ...
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
Passing Environment Variables with PM2
PM2 allows you to set environment variables for your application in several ways:
1. Using PM2 Ecosystem File:
Create a ecosystem.config.js
file in the root directory of your application and define environment variables within the env
property of your application configuration:
module.exports = {
apps: [
{
name: 'your-app-name',
script: 'app.js',
env: {
DB_HOST: 'your_database_host',
DB_USER: 'your_database_user',
DB_PASSWORD: 'your_database_password',
DB_DATABASE: 'your_database_name'
},
env_production: {
NODE_ENV: 'production',
DB_HOST: 'production_database_host',
// ... other environment-specific variables
}
}
]
};
2. Setting Environment Variables Globally:
You can set environment variables globally for all your PM2-managed applications:
pm2 set env DB_HOST 'your_database_host'
3. Setting Environment Variables for a Specific Application:
To set variables for a specific application:
pm2 set env your-app-name DB_USER 'your_database_user'
4. Setting Environment Variables During Launch:
You can pass environment variables directly when launching your application:
pm2 start app.js --env DB_HOST='your_database_host'
Key Considerations and Best Practices:
- Security: Never hardcode sensitive information (like database credentials) within your application code. Store them securely in environment variables or dedicated secrets management services.
- Environment-Specific Configuration: Use environment variables to separate configuration for different environments (development, testing, production) to ensure a consistent development workflow and prevent accidental deployment of sensitive information.
- .env Files: Consider using
.env
files to manage your environment variables locally and keep your code clean. - Secret Management Services: For production deployments, leverage dedicated secret management services like HashiCorp Vault or AWS Secrets Manager to store and manage sensitive information securely.
Conclusion:
Passing environment variables to your Node.js applications via PM2 enables a more flexible and secure approach to managing application configurations across different environments. By adopting best practices and utilizing environment variables effectively, you can simplify deployment, improve security, and ensure your application's stability.
Additional Resources: