Keywords: PM2 | Node.js | Environment Variables | Argument Passing | Configuration Files
Abstract: This article discusses various methods for passing execution arguments to Node.js applications managed by PM2, with a focus on the best practice of using environment variables such as NODE_ENV in combination with configuration files. It also covers PM2 features like the --node-args option and ecosystem configuration to enhance application configurability and deployment efficiency.
Problem Introduction
When using PM2 to start Node.js applications, developers may encounter issues with passing execution arguments. For example, users might try the command pm2 start app.js -- dev to pass arguments, but find it ineffective, whereas it works with forever. This article explores how to effectively pass arguments in PM2.
Direct Argument Passing Methods
Based on related answers, PM2 supports direct argument passing via the command line. One approach is to use the --node-args option to pass Node.js-specific arguments, for example:
pm2 start myServer.js --node-args="--production --port=1337"Another method is to add any arguments after --, such as:
pm2 start app.js -- --prod --second-arg --third-argThese methods are straightforward and suitable for quickly passing a small number of arguments.
Best Practice Using Environment Variables
However, for managing configurations across different environments (e.g., development, testing, production), using environment variables is a more elegant and maintainable approach. By setting environment variables like NODE_ENV, applications can load corresponding configuration files based on the variable value.
For instance, create a config.json file to define configurations for various environments:
{
"dev": {
"db": {
"hosts":["localhost"],
"database": "api"
},
"redis": {
"hosts": ["localhost"]
}
},
"staging": {
"db": {
"hosts":["1.1.1.1"],
"database": "api"
},
"redis": {
"hosts": ["2.2.2.2"]
}
},
"production": {
"db": {
"hosts":["1.1.1.1", "1.1.1.2", "1.1.1.3"],
"database": "api"
},
"redis": {
"hosts": ["2.2.2.2", "2.2.2.3"]
}
}
}In the application, retrieve the environment variable via process.env.NODE_ENV and load the corresponding configuration:
var config = require('./config.json')[process.env.NODE_ENV || 'dev'];Then, set the environment variable before starting the application, for example, in the shell:
export NODE_ENV=staging
pm2 start app.jsThis approach makes configuration management more flexible and allows leveraging PM2's ecosystem for setting environment variables.
Configuration Management and PM2 Ecosystem
PM2 provides ecosystem files (e.g., ecosystem.config.js) that allow defining environment variables and other configurations when starting applications. This is similar to parameter passing in POSIX system daemons, ensuring parameter persistence.
Furthermore, using environment variables avoids hardcoding arguments in the command line, improving application security and portability. Developers should prioritize using environment variables for passing execution arguments, especially in production environments.
Conclusion
In PM2, there are multiple methods for passing execution arguments: directly via command line using --node-args or adding arguments after --, or more recommendedly, using environment variables combined with configuration files. The environment variable approach offers better configurability and maintainability, suitable for managing settings across different environments. Combined with PM2's ecosystem, developers can achieve efficient application deployment and management.