I am using PM2 increment_var for PORT that is incrementing the port for a PM2 cluster. Without Doppler it is working, but with Doppler it just ignores the Port in the config (below). The expected behaviour would be to have 2 instances (one running at Port 8080 and one other at Port 8081).
Doing this is actually pretty easy out of the box. The issue you’re running into is that you’re defining the environment variable in your config file. Using an ecosystem file that looks like this will work:
Thats the same that I had, when I understand correctly what you mean. You can see my ecosystem.config.js in the original question. It did not work for me or did I forget to consider anything that you ment in your answer.
I found a difference in your answer in how you start the cluster. When I do that it increments in a strage way.
When I run a cluster with PM2 with 3 instances, then
the first instance has Port: 8080
the second instance has Port 80801
the third instance has Port 808011, which leads to an error like this:
uncaughtException RangeError: options.port should be >= 0 and < 65536. Received 808011.
Yep, you’re right. It looks like what’s happening here is that it’s not handling when PORT is set to a string properly. If you set it to an integer in the config file like this:
env: {
"PORT": 3000
}
Then it works as expected. However, if you instead set it to this:
env: {
"PORT": "3000"
}
to emulate the fact that all environment variables start as strings in process.env then you see the same behavior where port 3000 works fine but the incremented port is on 30001.
From my perspective, this seems like a bug in pm2. It looks like this bug was reported back in Jan 2021:
I recommend chiming in on that issue to show your support for it.
@Peer As an aside, I did take a look at the code surrounding this. I’m not an expert when it comes to PM2 or node, but it looked easy enough to solve, so I submitted a PR to fix this:
When I tried your approach I tried with a number and also with a string, so in my case 8080 and also “8080”, but it did not help, meaning the incremented-port stayed strange, like 80801 and 808011.
I am curious how it will run with the new PM2 version, with your changes inside . In the meantime I will stick to my workaround.