Heroku process.env.port is undefined

杀马特。学长 韩版系。学妹 提交于 2020-04-16 02:10:05

问题


I am trying to run a node.js app on heroku. I got it working local, but when i deploy it on heroku i get the following error:

Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

This is the port i try to listen to:

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

When i logged what the process.env.port was it said process.env.port was undefined.

Is there anything i need to do to automatically set the port?

Edit (FIX): So i found out where the problem was. The javascript was being minified, but the process.env.port was minified to something that didn't work. Thanks for the help.


回答1:


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



回答2:


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




回答3:


If you're using babel, do not use the babel plugin babel-plugin-transform-inline-environment-variables in Heroku. I think Heroku does not set the PORT variable when doing a deployment, so that's why you would get undefined.




回答4:


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


来源:https://stackoverflow.com/questions/39096995/heroku-process-env-port-is-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!