I have my first node.js app (runs fine locally) - but I am unable to deploy it via heroku (first time w/ heroku as well). The code is below. SO doesn\'t let me write so much
At of all the solution i have tried no one work as expected, i study heroku by default the .env File should maintain the convention PORT, the process.env.PORT, heroku by default will look for the Keyword PORT.
Cancel any renaming such as APP_PORT= instead use PORT= in your env file.
I realized that I don't need the port number in the request endpoint, so the endpoint was herokuapp.com
and not herokuapp.com:5000
.
The listen()
call can be without host and callback:
server.listen(5000);
In my case, I was using Babel with the babel-plugin-transform-inline-environment-variables plugin. Apparently, Heroku does not set the PORT env variable when doing a deployment, so process.env.PORT
will be replaced by undefined
, and your code will fallback to the development port which Heroku does not know anything about.
Edit package.json
:
...
"engines": {
"node": "5.0.0",
"npm": "4.6.1"
},
...
and Server.js
:
...
var port = process.env.PORT || 3000;
app.listen(port, "0.0.0.0", function() {
console.log("Listening on Port 3000");
});
...
For those that are passing both a port and a host, keep in mind that Heroku will not bind to localhost
.
You must pass 0.0.0.0
for host.
Even if you're using the correct port. We had to make this adjustment:
# port (as described above) and host are both wrong
const host = 'localhost';
const port = 3000;
# use alternate localhost and the port Heroku assigns to $PORT
const host = '0.0.0.0';
const port = process.env.PORT || 3000;
Then you can start the server, as usual:
app.listen(port, host, function() {
console.log("Server started.......");
});
You can see more details here: https://help.heroku.com/P1AVPANS/why-is-my-node-js-app-crashing-with-an-r10-error
change this line
app.listen(port);
to
app.listen(process.env.PORT, '0.0.0.0');
it will work