Developer hints: Why do my node.js app ports keep growing?

Working with NodeJS sometimes you get to see some strange things in your dev environment. For example, when working with Cloud Foundry your apps would automatically assign a port number to the running app and sometimes mess up your configuration when working with other people on the same project. But worry not, here's the answer. The (in)famous ports module.

Why do my Cloud Foundry app ports keep increasing exponentially?
~ Peter (2018)

Turns out the IBM platform-as-a-service platform using cloud foundry cli (now ibmcloud and before that also bluemix-cli or bx) uses a handy little node module called ports.

Where ports comes in is it assign your package.json app name to a port number and then remembers it whenever you run a new app. Where it also comes in handy is, whenever you run a new app, it increases the port number by 1 each time you run a new app that takes into account the cfenv.port ENVIRONMENT VARIABLE.

Once you dig through the code you find that the ports module stores the port numbers in a JSON file in your home folder - on Mac ~/.ports.json or if you're using Windows it would be under your %USERPROFILE%/.ports.json which would typically translate to c:\Users\Peter or something like that. You can use the ports.json file to assign your apps a PORT instead of running the EXPORT PORT=XXXX.

{
    "6001": {
        "name": "MicoDashboard"
    },
    "6003": {
        "name": "BikeStand"
    },
    "6004": {
        "name": "automation-workshop"
    },
    ...
    "6013": {
        "name": "Innovation Sandwich"
    }
}

Sample config you might find.

Have fun Noding. Cześć!