Heroku process.env.port is undefined Heroku process.env.port is undefined heroku heroku

Heroku process.env.port is undefined


for any one get to that post.first check if you are writing process.env.PORT correctly.I was writing .Port and it took 4 hours of my life to figure out the error


You should pass config args before run the server on heroku. In terminal type

$ heroku config:set PORT=3333

then you'll be able to get port number like this

const PORT = process.env.PORT || 3000;

If you assigned from terminal

 console.log(PORT) => 3333

otherwise

console.log(PORT) => 3000


I had the same issue and found the solution to pass heroku dyno port variable to node start script.

If you have created a Procfile in your root directory of your project just open it.

If you haven't created a Procfile just go to root directory of your project and create a file called "Procfile". The file has no extension and it helps to heroku get the starter point of your application. More details about it: The Procfile

In Node.JS Heroku documentation there isn't provided any example how should look like Procfile.

In my case, I'm using Typescript and my output directory of .js files is in /build directory in my project folder. and I'm assuming that you have the same situation. If not, just put your starter file's directory.

Procfile:

web: PORT=$PORT node ./build/index.js