NestJS + Heroku - Crashes immediately after a successful build without reason - heroku

Procfile
web: npm run start:prod
Package.json
"start:prod": "NODE_ENV=production node dist/main",
Heroku logs
2022-05-10T09:01:50.583126+00:00 app[web.1]: [Nest] 22 - 05/10/2022, 9:01:50 AM LOG [NestApplication] Nest application successfully started +42ms
2022-05-10T09:01:50.818469+00:00 heroku[web.1]: Process exited with status 1
2022-05-10T09:01:50.875464+00:00 heroku[web.1]: State changed from starting to crashed
Everything works fine on local (heroku local and npm run start), but crashes on Heroku. I've tried these before:
Listen on process.env.PORT || 8080
All environments are set (heroku run printenv)
Set 0.0.0.0/0 IP on MongoDB Atlas
These are the environments that Heroku adds:
MEMORY_AVAILABLE=512
PWD=/app
PORT=42675
NODE_ENV=production
NODE_HOME=/app/.heroku/node
LINES=17
HOME=/app
COLUMNS=150
SHLVL=0
WEB_MEMORY=512
WEB_CONCURRENCY=1
PS1=\[\033[01;34m\]\w\[\033[00m\] \[\033[01;32m\]$ \[\033[00m\]
PATH=/app/.heroku/node/bin:/app/.heroku/yarn/bin:/usr/local/bin:/usr/bin:/bin:/app/bin:/app/node_modules/.bin
DYNO=run.5170
I'm not sure, but could it be for minimum hardware specification?

The issue was using port 80 for gateway and http-session-affinity was not enabled as it's required for Socket.io. Set process.env.PORT for both HTTP and gateway.
To enable http-session-affinity, run heroku features:enable http-session-affinity

Related

How to run a node app with no listening port in Heroku

I have a node app that does a simple web scraping in real time.
Since it has no back-end feature, it doesn't listen any port and never use Express.
When I deploy this app, Heroku automatically runs some commands to build and run my app.
This is my package.json file.
"scripts": {
"develop": "nodemon --exec babel-node src",
"test": "mocha --require babel-core/register",
"build": "rimraf lib && babel src -d lib",
"start": "node lib",
"lint": "eslint src"
}
I checked the Heroku logs to see if the app is running properly but I found errors like below.
...
2020-12-27T07:24:48.000000+00:00 app[api]: Build succeeded
2020-12-27T07:25:46.564866+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-12-27T07:25:46.589773+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-12-27T07:25:46.680704+00:00 heroku[web.1]: Process exited with status 137
2020-12-27T07:25:46.732427+00:00 heroku[web.1]: State changed from starting to crashed
2020-12-27T07:25:46.735872+00:00 heroku[web.1]: State changed from crashed to starting
2020-12-27T07:25:51.844095+00:00 heroku[web.1]: Starting process with command `npm start`
2020-12-27T07:25:54.728760+00:00 app[web.1]:
2020-12-27T07:25:54.728776+00:00 app[web.1]: > my-node-app#0.1.0 start /app
2020-12-27T07:25:54.728777+00:00 app[web.1]: > node lib
2020-12-27T07:25:54.728777+00:00 app[web.1]:
2020-12-27T07:26:52.296540+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-12-27T07:26:52.315003+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-12-27T07:26:52.410290+00:00 heroku[web.1]: Process exited with status 137
2020-12-27T07:26:52.468187+00:00 heroku[web.1]: State changed from starting to crashed
It seems building was successful but has failed when running it.
In my opinion, Heroku tried to bind PORT but my app never listened any port, so these errors had occurred.
How can I run my app successfully with no error on Heroku in this case?
If your node.js project is a web server, you cannot remove the port. No port, no web server. Trying to make a web server without a port is like trying to make a locomotive with no railroad.
You can use the default port however. When users give browsers URLs without ports, they automatically apply the default port.
Heroku also provide the default port so you don't have to specify the port
Just deploy your app and use the url provided by the heroku to call node js application

Nuxt App deployed to Heroku doesn't find sass-loader

I'm trying to deploy a Nuxt SPA app to Heroku.
Locally everything works fine, but when I deploy to Heroku it gives me an application error with the info to check my heroku logs:
2019-10-11T02:49:50.897584+00:00 heroku[web.1]: State changed from crashed to starting
2019-10-11T02:49:54.670233+00:00 heroku[web.1]: Starting process with command `npm start`
2019-10-11T02:49:56.590749+00:00 app[web.1]:
2019-10-11T02:49:56.590769+00:00 app[web.1]: > sol37.web#1.0.0 start /app
2019-10-11T02:49:56.59077+00:00 app[web.1]: > nuxt start
2019-10-11T02:49:56.590772+00:00 app[web.1]:
2019-10-11T02:49:57.167804+00:00 app[web.1]: ℹ Listening on: http://localhost:49791/ 2019-10-11T02:50:02.00478+00:00 heroku[web.1]: source=web.1 dyno=heroku.149207054.9b940788-6a37-40a9-82de-b19b3519e60c sample#memory_total=55.14MB sample#memory_rss=55.14MB sample#memory_cache=0.00MB sample#memory_swap=0.00MB sample#memory_pgpgin=14487pages sample#memory_pgpgout=1392pages sample#memory_quota=512.00MB
2019-10-11T02:50:23.81059+00:00 heroku[web.1]: source=web.1 dyno=heroku.149207054.9b940788-6a37-40a9-82de-b19b3519e60c sample#memory_total=55.14MB sample#memory_rss=55.14MB sample#memory_cache=0.00MB sample#memory_swap=0.00MB sample#memory_pgpgin=14487pages sample#memory_pgpgout=1392pages sample#memory_quota=512.00MB
2019-10-11T02:50:45.744385+00:00 heroku[web.1]: source=web.1 dyno=heroku.149207054.9b940788-6a37-40a9-82de-b19b3519e60c sample#load_avg_1m=0.00
2019-10-11T02:50:45.762243+00:00 heroku[web.1]: source=web.1 dyno=heroku.149207054.9b940788-6a37-40a9-82de-b19b3519e60c sample#memory_total=55.14MB sample#memory_rss=55.14MB sample#memory_cache=0.00MB sample#memory_swap=0.00MB sample#memory_pgpgin=14487pages sample#memory_pgpgout=1392pages sample#memory_quota=512.00MB
2019-10-11T02:50:54.906637+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
I checked if I set the port somewhere manually, but I'm not doing that anywhere in the project.
Without more information, i tried to build the app on heroku directly.
("heroku run bash" and "npm run build") which gave me the following error.
ERROR in ./pages/index.vue
Module not found: Error: Can't resolve 'sass-loader' in '/app'
and 7 more of those errors all concerning sass-loader.
not sure if "npm run build" works over heroku bash, but at least it gave me some information.
I have another nuxt project that i run on the same heroku instance which works without a problem.
I tried to check what the differences are and i found that it used a lower sass-loader version, which i downgraded on my current project but it's still not working.
After that I tried to run "npm run start" which is what Heroku runs upon deployment. this gave me first an error with nuxtjs/style-ressource, which i realized i don't utilize right now, so i removed that. After that it ran without any issues, but i couldn't get it to show up in the browser.
Does anyone have any idea what else I could try?
Thanks in Advance
Carlo

Heroku - crash all the time

I am developing node.js app on heroku. For now it runs on single (free) dyno.
For some reason suddenly my app crashed and now it crashes all the time (I observed it after adding NewRelic and Librato addons - app is kind of restarted when adding those addons) - app crashed for the first time after one of those addons were added. So I removed both addons but problem still remained. I wanted to check whats wrong and I commented my app code and replaced it with simple example from the web:
index.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(process.env.PORT);
console.log('Server running at http://127.0.0.1:1337/');
Procfile
web: node index.js
engines in packages.json (node installed by heroku is 0.10.26)
"engines": {
"node": "0.10.x"
},
This code works on my pc (tested with foreman).
When I try to deploy it to heroku app crashes - here is the log:
2014-04-25T09:43:42+00:00 heroku[slug-compiler]: Slug compilation started
2014-04-25T09:43:47.850609+00:00 heroku[api]: Release v30 created by xxx
2014-04-25T09:43:47.850538+00:00 heroku[api]: Deploy 562babb by xxx
2014-04-25T09:43:47+00:00 heroku[slug-compiler]: Slug compilation finished
2014-04-25T09:43:48.588089+00:00 heroku[web.1]: State changed from crashed to starting
2014-04-25T09:43:55.655057+00:00 heroku[web.1]: Starting process with command `node index.js`
2014-04-25T09:43:57.931274+00:00 heroku[web.1]: Process exited with status 8
2014-04-25T09:43:57.945393+00:00 heroku[web.1]: State changed from starting to crashed
when I try to heroku restart:
2014-04-25T09:44:43.071357+00:00 heroku[web.1]: State changed from crashed to starting
2014-04-25T09:44:51.834860+00:00 heroku[web.1]: Starting process with command `node index.js`
2014-04-25T09:44:54.250631+00:00 heroku[web.1]: State changed from starting to crashed
2014-04-25T09:44:54.235545+00:00 heroku[web.1]: Process exited with status 8
This make me crazy - I deployed many node apps to heroku which are runing in production and had never such problems - whats going on???
When I changed node engine version to 0.10.20 (I am using this v localy) then app started and was working but when I did heroku restart it crashed again...
State changed from up to starting
2014-04-25T10:10:12.990317+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2014-04-25T10:10:15.145758+00:00 heroku[web.1]: Process exited with status 143
2014-04-25T10:10:16.151380+00:00 heroku[web.1]: Starting process with command `node index.js`
2014-04-25T10:10:18.905637+00:00 heroku[web.1]: Process exited with status 8
2014-04-25T10:10:18.929730+00:00 heroku[web.1]: State changed from starting to crashed
after second restart app was up and runing again, and after third restart it crashed again (it always crashes/exits with status 8).
The problem could be on Heroku itself. There was an incident report created about an hour ago but it is still unclear what is causing the problem:
https://status.heroku.com/incidents/614
Meanwhile, you might want to rollback to the previous working build of your app via HerokuDashboard.
add this config to package.json:
"scripts": {
"start": "node index.js"},
index.js is name of of file you want to start you app from here like www.js or server.js
www.piero.ir/en

Deploying Flask with Heroku

I'm trying to deploy a Flask app to Heroku however upon pushing the code I get the error
2013-06-23T11:23:59.264600+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
I'm not sure what to try, I've tried changing the port from 5000 to 33507, but to no avail. My Procfile looks like this:
web: python main.py
main.py is the main Flask file which initiates the server.
Thanks.
In my Flask app hosted on Heroku, I use this code to start the server:
if __name__ == '__main__':
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
When developing locally, this will use port 5000, in production Heroku will set the PORT environment variable.
(Side note: By default, Flask is only accessible from your own computer, not from any other in the network (see the Quickstart). Setting host='0.0.0.0' will make Flask available from the network)
In addition to msiemens's answer
import os
from run import app as application
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
application.run(host='0.0.0.0', port=port)
Your Procfile should specify the port address which in this case is stored in the heroku environment variable ${PORT}
web: gunicorn --bind 0.0.0.0:${PORT} wsgi
Your main.py script cannot bind to a specific port, it needs to bind to the port number set in the $PORT environment variable. Heroku sets the port it wants in that variable prior to invoking your application.
The error you are getting suggests you are binding to a port that is not the one Heroku expects.
This also fixes the problem of H20: App boot timeout.
My Procfile looks like this:
web: gunicorn -t 150 -c gunicorn_config.py main:app --bind 0.0.0.0:${PORT}
and main.py:
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)

Heroku Boot Timeout (Error R10)

Every time I launch my app it cannot get past the 60 second point without:
2012-05-06T22:41:11+00:00 heroku[web.1]: Stopping process with SIGKILL
2012-05-06T22:41:11+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2012-05-06T22:41:11+00:00 heroku[web.1]: Process exited with status 137
2012-05-06T22:41:12+00:00 heroku[web.1]: State changed from starting to crashed
Here is my Procfile:
web: bundle exec thin start -p $PORT
Any responses will be thoroughly appreciated.
If your app does take longer than 60 seconds for "good" reasons, you can work around the 60s boot time limit with https://github.com/dblock/heroku-forward.
The solution was that I had forgotten to include the -p $PORT in my Procfile line.
in Procfile change:
web: bundle exec thin start
to
web: bundle exec thin start -p $PORT
That fixed it for me.
Heroku's boot timeout bit me too. I read several blog posts about how to get around it and ended up automating some of the solutions into a gem.
To reduce the startup time on deploy, you can trim the gems loaded at boot time (this doesn't mean you have to trim them from the app, just boot time).
gem_bench evaluates which gems are likely to not be needed at boot time.
I have an app with about 250 gems and was able to add :require => false to about 60 of them, with dramatic effects.
https://github.com/acquaintable/gem_bench
Disclaimer: I am the author of this open source ruby gem. I wrote the gem to aid myself in solving this exact problem: the 60 second timeout on Heroku.
Hi i was facing the same issue.I have resolved this issue by increase the timeout in /config/unicorn.rb
change timeout 15 to timeout 20 in /config/unicorn.rb
In my case using nodejs I solved this adding a Procfile file with content:
worker: node index.js and push it to heroku.
After that make sure to disable the check "web npm start" and turn on the check "worker node index.js" just like the image attached below
herokuResourcesConfig
I was having the same error when deploying my Node app on Heroku.
I got it solved by adding a Procfile.
web: node app.js
It tells Heroku how to start the application.
The error is because of Heroku is not able to configure on which PORT to run the application.
It can be solved by specifying the PORT for Heroku, ie: in app.js
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`App is running on port ${ PORT }`);
});
Error R10 (Boot timeout)
is this hidden section of heroku allows you to increase the deployment time.
https://tools.heroku.support/limits/boot_timeout
I got this error because Heroku didn't have access to the Mongo Atlas database. You need to change this in the database settings
have the same issue, solved by creating file with proxy server
https://www.npmjs.com/package/http-proxy#setup-a-basic-stand-alone-proxy-server
proxy.js:
httpProxy.createProxyServer({
target, // target that can't cant be exposed, e.g. localhost:4000
changeOrigin: true,
}).listen(process.env.PORT); // port from heroku runtime
then
node server/proxy.js

Resources