How to specify a port number for pm2
Asked Answered
I

3

47

I'm trying to use pm2 to manage a node.js cluster

pm2 start . -i 3

I'm currently running the app on heroku and using a Procfile with the above command, but I cannot figure out how to configure pm2 to use the existing PORT env var. Something like pm2 start . -p $PORT

What am I missing?

Irreversible answered 19/7, 2015 at 14:38 Comment(0)
S
57

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.

Speechmaker answered 19/7, 2015 at 15:58 Comment(8)
Thanks @stdob. I don't need to access an env var in code. I need to configure pm2 to start node instances on specified ports.Irreversible
@lukewendling What you mean: "start node instances on specified port"?Speechmaker
Can you please help me with how to run in port 80.. This is what I'm trying to do. $ PORT=80 pm2 start ./bin/www --name sample --env production -i -1Jone
@Anoop.P.A Sorry its a bit late for your comment. Hopefully you figured it out. If not, when running in a local environment on a linux/unix machine, ports below 1024 require sudo. So you would want to do PORT-80 sudo pm2 start ...Airedale
You can use : pm2 start /path/of/app.js -i 4 -- --port=1336Chili
I get below error as, $ PORT=3002 pm2 start -I 0 main.js error: unknown option `-I'Valley
@Valley It was a mistake with parameter capitalization. Fixed))Speechmaker
@stdob--, I've configured port via ecosystem.json but the app is still not reachable. Can you, please, have a look: stackoverflow.com/questions/68109551Tablespoon
E
16

You need to use -- to tell pm2 to stop parsing his options and give the rest to the program, then when you spawn direct binary, you need to tell pm2 that you don't want to use nodejs, so :

pm2 start rethinkdb --interpreter none -- --port 8082

You see you need -- --port 8082

Eachelle answered 12/4, 2019 at 7:7 Comment(4)
Please send me entire line where you start your pm2 instance.Eachelle
This works fine pm2 start dev -- --port 3100. Just make sure you have extra -- before --port. I used to get into confusion with env variables now and then myself, this fixes it for me.Nigro
nah, it doesnt work, there is only 'NODE_PORT=3002 pm2 start ecosystem.config.js --no-daemon' prints process.env.NODE_PORT. In ur solution port doesnt exist in env or argvYehudit
my simply version ecosystem.config.js: module.exports = { apps : [{ name: 'name', script: './bin/www', node_args: '-r esm', cwd: './server' }] }Yehudit
A
6

An easy way of telling your server application on which port to run is through PM2's

ecosystem configuration files

in conjunction with properly configured use of $PORT environment vars inside your server application. That means your server reads $PORT environment var to start the server or microservice on specified port.

There are different formats available you can choose for the file to have. I personally use the CommonJS module format (amongst other options are JSON and YAML).

Inside ecosystem.config.js you specify one entry object for each server instance you want to launch through PM2.

The point is that you can also specify environment vars for the different processes and that way you can setup $PORT for all the processes. The following is an example config for three different processes.

    module.exports = {
  apps : [
  {
    name      : "Main API server",
    script    : "./backend/dist/main.js",
    instances : "2",
    exec_mode : "cluster",
    env: {
      NODE_ENV: "production",
      PORT: 4300    
    }
  },
  {
    name      : "Worker server 1",
    script    : "./backend-worker/dist/main.js",
    instances : "1",
    exec_mode : "fork",
    env: {
      PORT: 4000,
    },
    
  },
  {
    name      : "Worker server 2",
    script    : "./backend-worker/dist/main.js",
    instances : "1",
    exec_mode : "fork",
    env: {
      PORT: 4001,
    }
  },
  ]
}

One note: This configuration uses PM2 as a loadbalancer for the first process that runs as cluster on two cores. The other (worker-)processes are run each on the specified port.

An example snippet of server startup code using the environment $PORT var for a NodeJS server is following:

// ...
const port = (process.env.PORT) ? process.env.PORT : 4300

console.log('$PORT: ', port)

const server = await app.listen(port, '0.0.0.0')
// ...

When you have all in place you simply call following to startup your servers:

pm2 start ecosystem.config.js
Affenpinscher answered 29/10, 2020 at 16:11 Comment(3)
if you define a port (e.g. 4300 listed above) and then have 2 instances of that process, doesn't that mean you'll get a "port in use" error ? Does PM2 cluster-mode have some way to deal with this ?Alleviative
It's a kind of port-forwarding, right ? So, does that mean it containerizes the application to avoid the port collision? If you would look at the pstree, are the processes listed directly, or are they wrapped inside some kind of PM2 process ? And how does it know which environment variables are ports. I mean, this time the parameter is called "PORT", but it could as well be called "FOO_BAR". Would it still detect and loadbalance it ?Alleviative
I am not sure if thats really port-forwarding but under the hood PM2 uses NodeJS cluster module to share one port between child processes. PM2 does the needed config out of the box so you dont need to write any code for that feature.Affenpinscher

© 2022 - 2024 — McMap. All rights reserved.