Search code examples
node.jsheroku

Heroku returning error 'Web process failed to bind to $PORT within 60 seconds of launch' error despite using process.env.PORT, what to do?


This is my NodeJS server

const fastify = require('fastify');
const JWT = require('jsonwebtoken');

const server_manifest = require('./manifest.json');

(async ()=>{
    const server = fastify(server_manifest.fastify_options)

    const routes = await require('./routes/index')();
    routes.forEach( route => {
        server.route(route)
    })

    const server_port = process.env.PORT || 80
    await server.listen(server_port)
    console.log(`Listening to port ${server_port}`)
} )()

I am using Heroku CLI to deploy my application.

After pushing the repo to Heroku I it return build successful, but the server fails to bind with any port, on heroku.

Logs from heroku:

2020-10-14T19:34:11.000000+00:00 app[api]: Build started by user [email protected]
2020-10-14T19:34:24.744074+00:00 app[api]: Release v8 created by user [email protected]
2020-10-14T19:34:24.744074+00:00 app[api]: Deploy c5ce6232 by user [email protected]
2020-10-14T19:34:25.000000+00:00 app[api]: Build succeeded
2020-10-14T19:34:25.420753+00:00 heroku[web.1]: State changed from crashed to starting
2020-10-14T19:34:27.634828+00:00 heroku[web.1]: Starting process with command `npm start`
2020-10-14T19:34:30.313002+00:00 app[web.1]:
2020-10-14T19:34:30.313020+00:00 app[web.1]: > [email protected] start /app
2020-10-14T19:34:30.313021+00:00 app[web.1]: > node server.js
2020-10-14T19:34:30.313021+00:00 app[web.1]:
2020-10-14T19:34:30.674062+00:00 app[web.1]: {"level":30,"time":1602704070673,"pid":23,"hostname":"ed54244a-ed04-45a8-903e-428a8b57f725","msg":"Server listening at http://127.0.0.1:17850"}
2020-10-14T19:34:30.676814+00:00 app[web.1]: Listening to port 17850
2020-10-14T19:35:27.702639+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-10-14T19:35:27.736001+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-10-14T19:35:27.873461+00:00 heroku[web.1]: Process exited with status 137
2020-10-14T19:35:27.923509+00:00 heroku[web.1]: State changed from starting to crashed
2020-10-14T19:35:27.926649+00:00 heroku[web.1]: State changed from crashed to starting
2020-10-14T19:35:42.981476+00:00 heroku[web.1]: Starting process with command `npm start`
2020-10-14T19:35:48.230394+00:00 app[web.1]:
2020-10-14T19:35:48.230408+00:00 app[web.1]: > [email protected] start /app
2020-10-14T19:35:48.230409+00:00 app[web.1]: > node server.js
2020-10-14T19:35:48.230409+00:00 app[web.1]:
2020-10-14T19:35:50.170208+00:00 app[web.1]: {"level":30,"time":1602704150142,"pid":23,"hostname":"bc2dd20b-2d49-4db4-b62a-cc2d34bfffdb","msg":"Server listening at http://127.0.0.1:28541"}
2020-10-14T19:35:50.186877+00:00 app[web.1]: Listening to port 28541
2020-10-14T19:36:43.479233+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-10-14T19:36:43.504937+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-10-14T19:36:43.625303+00:00 heroku[web.1]: Process exited with status 137
2020-10-14T19:36:43.678738+00:00 heroku[web.1]: State changed from starting to crashed
2020-10-14T19:36:46.409789+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=jovialblogs.herokuapp.com request_id=aca75a5a-a6ee-45bf-8f36-69b17d41c731 fwd="49.36.133.90" dyno= connect= service= status=503 bytes= protocol=https
2020-10-14T19:36:47.606655+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=jovialblogs.herokuapp.com request_id=3ad806e9-2782-4501-9aea-ebe732e0e93d fwd="49.36.133.90" dyno= connect= service= status=503 bytes= protocol=https

Solution

  • You should manually specify '0.0.0.0' host, e.g.:

    const host = '0.0.0.0';
    await server.listen(server_port, host);