How to specify a port number for pm2

You can use an environment variable.
For example:

  1. NODE_PORT=3002 pm2 start -i 0 app.js

  2. Here is how to read the value in app:

console.log(process.env.NODE_PORT);

Or, if you are building an Express app:

  1. PORT=3002 pm2 start -i 0 ./bin/www

  2. Express loads PORT automatically when the application starts.

Leave a Comment