Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

failed to bind to $PORT in Heroku running environment #2176

Closed
tonychangcheng opened this issue Dec 20, 2018 · 13 comments
Closed

failed to bind to $PORT in Heroku running environment #2176

tonychangcheng opened this issue Dec 20, 2018 · 13 comments
Assignees

Comments

@tonychangcheng
Copy link

tonychangcheng commented Dec 20, 2018

Description / Steps to reproduce / Feature proposal

  1. Create a new lb4 app
  2. Move all dev dependencies to dependencies
  3. Connect heroku to your github repo
  4. The app is deployed successfully
  5. But when you hit the api, it fails.

Current Behavior

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

Expected Behavior

The port should be bind correctly.

The repo is here https://github.com/jiangren-mel-fullstack-1812/cheng-lb4-order-api

@dhmlau
Copy link
Member

dhmlau commented Dec 20, 2018

@hacksparrow, do you think you can help here? Thanks.

@hacksparrow
Copy link
Contributor

Taking a look.

@hacksparrow
Copy link
Contributor

hacksparrow commented Dec 25, 2018

Create a file named Procfile in the root of the project with the following content:

web: node index.js

@bajtos
Copy link
Member

bajtos commented Jan 11, 2019

FWIW, it would be great to have a Heroku Deployment guide in our docs, where we can describe these specific steps.

@tonychangcheng
Copy link
Author

@hacksparrow I added the Procfile and then I got another error. Did you see this error?

2019-01-17T06:57:03.170917+00:00 heroku[web.1]: Starting process with command node index.js
2019-01-17T06:57:06.802921+00:00 heroku[web.1]: State changed from starting to crashed
2019-01-17T06:57:06.781810+00:00 heroku[web.1]: Process exited with status 1
2019-01-17T06:57:06.718073+00:00 app[web.1]: internal/modules/cjs/loader.js:605
2019-01-17T06:57:06.718088+00:00 app[web.1]: throw err;
2019-01-17T06:57:06.718090+00:00 app[web.1]: ^
2019-01-17T06:57:06.718091+00:00 app[web.1]:
2019-01-17T06:57:06.718093+00:00 app[web.1]: Error: Cannot find module './dist'
2019-01-17T06:57:06.718095+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:603:15)
2019-01-17T06:57:06.718096+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:529:25)
2019-01-17T06:57:06.718098+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:657:17)
2019-01-17T06:57:06.718099+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:22:18)
2019-01-17T06:57:06.718101+00:00 app[web.1]: at Object. (/app/index.js:1:83)
2019-01-17T06:57:06.718102+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:721:30)
2019-01-17T06:57:06.718104+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
2019-01-17T06:57:06.718105+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:620:32)
2019-01-17T06:57:06.718107+00:00 app[web.1]: at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
2019-01-17T06:57:06.718108+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:552:3)

@hacksparrow
Copy link
Contributor

Error: Cannot find module './dist'

That means the ./dist directory is missing, you should have a script to build the project using a prestart script. Eg: https://github.com/strongloop/loopback-next/blob/master/examples/hello-world/package.json#L27.

@dhmlau
Copy link
Member

dhmlau commented Jan 17, 2019

FWIW, it would be great to have a Heroku Deployment guide in our docs, where we can describe these specific steps.

+1.
@tonychangcheng , would you like to contribute to this deployment guide? I'm thinking it would be under the How-to section of the docs. FYI - all LB4 docs are located in https://github.com/strongloop/loopback-next/tree/master/docs/site.

@dhmlau dhmlau reopened this Jan 17, 2019
@tonychangcheng
Copy link
Author

@hacksparrow The default behaviour is to run "npm start" which would run "prestart" before "start". As you can see from the log. The issue is the $Port was not bind correctly. I don't think a Procfile would fix the port issue.

Can you update the official Hello world example so that it can be just connected by Heroku and running on Heroku?

@dhmlau I can do that when the issue is fixed.

2019-01-17T23:58:17.092418+00:00 heroku[web.1]: Starting process with command npm start
2019-01-17T23:58:18.000000+00:00 app[api]: Build succeeded
2019-01-17T23:58:19.926093+00:00 app[web.1]:
2019-01-17T23:58:19.926123+00:00 app[web.1]: > cheng-lb4-order-api@1.0.0 prestart /app
2019-01-17T23:58:19.926125+00:00 app[web.1]: > npm run build
2019-01-17T23:58:19.926126+00:00 app[web.1]:
2019-01-17T23:58:20.270887+00:00 app[web.1]:
2019-01-17T23:58:20.271061+00:00 app[web.1]: > cheng-lb4-order-api@1.0.0 build /app
2019-01-17T23:58:20.271105+00:00 app[web.1]: > lb-tsc es2017 --outDir dist
2019-01-17T23:58:20.271108+00:00 app[web.1]:
2019-01-17T23:58:28.767980+00:00 app[web.1]:
2019-01-17T23:58:28.767999+00:00 app[web.1]: > cheng-lb4-order-api@1.0.0 start /app
2019-01-17T23:58:28.768000+00:00 app[web.1]: > node .
2019-01-17T23:58:28.768001+00:00 app[web.1]:
2019-01-17T23:58:30.384356+00:00 app[web.1]: Server is running at http://127.0.0.1:53584
2019-01-17T23:58:30.384742+00:00 app[web.1]: Try http://127.0.0.1:53584/ping
2019-01-17T23:59:17.333145+00:00 heroku[web.1]: State changed from starting to crashed
2019-01-17T23:59:17.199590+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-01-17T23:59:17.199590+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-01-17T23:59:17.319490+00:00 heroku[web.1]: Process exited with status 137

@quaywin
Copy link

quaywin commented Jan 18, 2019

Hi @tonychangcheng , you should try to change config in index.js
from
host: process.env.HOST || 'localhost'
to
host: process.env.HOST || '0.0.0.0'
It worked for me

@tonychangcheng
Copy link
Author

@quaywin Thanks for your comments. It worked for me as well.

@adesege
Copy link

adesege commented Feb 14, 2019

Just for posterity sake, you should only build your application at the build/deploy stage. You can have heroku-postbuild: npm run build in your package.json scripts to build the application immediately after depedencies have been installed. Then use npm start to start it.

You will still get the failed to bind to $PORT error if the application takes time to build when running it in the start stage.

See Running long build processes as part of npm run start in https://help.heroku.com/P1AVPANS/why-is-my-node-js-app-crashing-with-an-r10-error

@dhmlau
Copy link
Member

dhmlau commented Feb 14, 2019

@adesege, thanks for the info. Would you like to contribute to our docs on deploying to Heroku? I'm thinking of putting it under "How To": https://loopback.io/doc/en/lb4/How-tos.html. Thanks.

@senthilkumarpn
Copy link

Hi @tonychangcheng / @quaywin

i tried as per your previous comment to set host in index.js but still i'm getting localhost in heroku deployment.

Could you please help me on this.

2020-09-20T16:49:29.516050+00:00 app[web.1]: [1] In Recommender Package 0.0.0.0 60148
2020-09-20T16:49:29.541370+00:00 app[web.1]: [1] Recommendation REST server is running at http://127.0.0.1:60148.
2020-09-20T16:49:31.422166+00:00 app[web.1]: [0] In Shopping Package undefined 53420
2020-09-20T16:49:31.423031+00:00 app[web.1]: [0] undefined
2020-09-20T16:49:31.432999+00:00 app[web.1]: [0] undefined 53420
2020-09-20T16:49:31.433001+00:00 app[web.1]: [0] undefined undefined false
2020-09-20T16:49:41.267263+00:00 heroku[web.1]: State changed from starting to up
2020-09-20T16:49:41.190461+00:00 app[web.1]: [0] Server is running at https://127.0.0.1:53420
2020-09-20T16:49:41.193103+00:00 app[web.1]: [0] Try https://127.0.0.1:53420/ping
2020-09-20T16:49:41.194241+00:00 app[web.1]: [0] 0.0.0.0 53420 true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants