Heroku + node.js error (Web process failed to bind to $PORT within 60 seconds of launch)

前端 未结 24 1988
梦如初夏
梦如初夏 2020-11-22 05:42

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

相关标签:
24条回答
  • 2020-11-22 06:09

    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.

    0 讨论(0)
  • 2020-11-22 06:10

    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);
    
    0 讨论(0)
  • 2020-11-22 06:11

    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.

    0 讨论(0)
  • 2020-11-22 06:11

    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");
    });
    ...
    
    0 讨论(0)
  • 2020-11-22 06:12

    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

    0 讨论(0)
  • 2020-11-22 06:15

    change this line

    app.listen(port);
    

    to

    app.listen(process.env.PORT, '0.0.0.0');
    

    it will work

    0 讨论(0)
提交回复
热议问题