You can use an environment variable.
For example:
-
NODE_PORT=3002 pm2 start -i 0 app.js
-
Here is how to read the value in app:
console.log(process.env.NODE_PORT);
Or, if you are building an Express app:
-
PORT=3002 pm2 start -i 0 ./bin/www
-
Express loads
PORT
automatically when the application starts.