Specifying Port Numbers in PM2: Environment Variables and Configuration Explained

Dec 08, 2025 · Programming · 12 views · 7.8

Keywords: PM2 | port configuration | environment variables

Abstract: This article provides an in-depth analysis of how to specify port numbers in PM2, particularly in cloud platforms like Heroku. Based on Q&A data, it explains methods using environment variables (e.g., NODE_PORT or PORT) for configuration, with examples for Node.js and Express applications. Additionally, it discusses alternative options, such as using -- parameters to pass port settings, to aid developers in flexible application deployment. Key topics include reading environment variables, parsing PM2 commands, and best practices for cross-platform configuration.

Application of Environment Variables in PM2 Port Configuration

When deploying Node.js applications with PM2 for process management, specifying port numbers is a common requirement. Especially in cloud platforms like Heroku, ports are often dynamically assigned via environment variables (e.g., PORT). According to the Q&A data, the best practice is to configure ports using environment variables. For instance, when starting PM2, you can set the NODE_PORT or PORT environment variable, such as: NODE_PORT=3002 pm2 start -i 0 app.js. This approach allows the application to read the environment variable value at runtime, for example, using process.env.NODE_PORT in Node.js code to retrieve the port number.

Automatic Port Loading in Express Applications

For applications based on the Express framework, port configuration is further simplified. Express applications automatically load the PORT environment variable upon startup, requiring no additional code handling. Therefore, in the PM2 start command, you can directly use PORT=3002 pm2 start -i 0 ./bin/www. This ensures seamless integration into platforms like Heroku, where the PORT variable is provided by the platform. This design enhances configuration flexibility and cross-environment compatibility.

Other Configuration Options and Parameter Parsing

Beyond environment variables, the Q&A data also mentions using the -- parameter to pass port settings. For example, pm2 start rethinkdb --interpreter none -- --port 8082. Here, -- is used to instruct PM2 to stop parsing its own options and pass subsequent arguments to the target program. This is useful for non-Node.js applications or scenarios requiring direct parameter passing. However, this method scored lower (2.9 points), possibly due to its narrower applicability or lower usability compared to the environment variable approach.

Practical Recommendations and Conclusion

Summarizing the Q&A data, it is recommended to prioritize using environment variables for configuring PM2 port numbers, as they offer better maintainability and platform compatibility. In Heroku environments, ensure the Procfile correctly references the PORT variable, e.g., web: pm2 start . -i 3, and let the application code automatically read this variable. For complex scenarios, consider combining with -- parameters, but be mindful of their limitations. By understanding these core concepts, developers can manage Node.js cluster deployments more efficiently.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.