Heroku Node app keeps crashing - node.js

I have an Express app hosted on Heroku. Locally it runs just fine, but keeps crashing on Heroku. Every time I go to it I get the 'Application Error' page.
I double checked all environment variables and they seem ok.
The Heroku logs don't exactly offer much help:
2017-04-16T23:02:49.001768+00:00 heroku[web.1]: State changed from crashed
to starting
2017-04-16T23:02:52.072906+00:00 heroku[web.1]: Starting process with
command `npm run start`
2017-04-16T23:02:56.042611+00:00 app[web.1]:
2017-04-16T23:02:56.042628+00:00 app[web.1]: > my-app#0.0.1 start /app
2017-04-16T23:02:56.042629+00:00 app[web.1]: > node bin/app
2017-04-16T23:02:56.042630+00:00 app[web.1]:
2017-04-16T23:02:57.200912+00:00 heroku[web.1]: State changed from starting
to crashed
2017-04-16T23:02:57.201454+00:00 heroku[web.1]: State changed from crashed
to starting
2017-04-16T23:02:57.192198+00:00 heroku[web.1]: Process exited with status 0
2017-04-16T23:03:00.981528+00:00 heroku[web.1]: Starting process with
command `npm run start`
2017-04-16T23:03:04.312475+00:00 app[web.1]:
2017-04-16T23:03:04.312489+00:00 app[web.1]: > my-app#0.0.1 start /app
2017-04-16T23:03:04.312490+00:00 app[web.1]: > node bin/app
2017-04-16T23:03:04.312491+00:00 app[web.1]:
2017-04-16T23:03:06.093211+00:00 heroku[web.1]: Process exited with status 0
2017-04-16T23:03:06.112842+00:00 heroku[web.1]: State changed from starting
to crashed
So then I tried starting the app manually:
$ heroku run bash
...
$ npm run start
I got no errors. I'm using the pm2 process manager, so I checked it to see if the app is running and it seems like it is:
$ node_modules/.bin/pm2 list
...
But then when I go to open my app I get the same 'Application error' page as before.
'heroku restart' doesn't help. It just crashes again every time.
The only difference between my local environment and Heroku (as far as I can tell at least...) is that locally I have a file from which I'm reading a RSA private key, and on Heroku that key is read from the environment.
Edit: As requested, here's my Procfile:
web: npm run start
Any ideas?

Your app need to start listening to the port defined in the env. For example if you have an expressjs app it should look something like this:
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
app.get('/', (req, res) => res.send("Hello world"))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
If the app does not bind to the correct port, heroku is going to consider the app unhealthy and will terminate it.
You need to see this line in the logs, which indicates that heroku recognized the app as healthy:
State changed from starting to up
Keep in mind that this port thing is a special requirement for the web process. The other processes in your app does not have such a requirement.

Try 2 things:
Change your procfile:
web: node start.js
Then make sure that your web process it running by scaling it up:
heroku ps:scale web=1
On your computer in a terminal window run this command in your project:
heroku local web
It simulates how Heroku runs your app and should give you information of what is happening.

Related

Deploying a node.js app (discord.jd bot) on Heroku causing an error

I'm trying to deploy a discord bot on heroku. It works for about 1-2 minutes and then there is an error:
2020-08-10T10:05:17.228802+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-08-10T10:05:17.246146+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-08-10T10:05:17.307500+00:00 heroku[web.1]: Process exited with status 137
2020-08-10T10:05:17.347445+00:00 heroku[web.1]: State changed from starting to crashed
(complete log):
2020-08-10T10:04:19.372794+00:00 app[web.1]:
2020-08-10T10:04:19.372804+00:00 app[web.1]: > arepee-bot#1.0.0 start /app
2020-08-10T10:04:19.372804+00:00 app[web.1]: > node index.js
2020-08-10T10:04:19.372804+00:00 app[web.1]:
2020-08-10T10:04:20.468443+00:00 app[web.1]: Bot [welcomeMessage.js] is ready!
2020-08-10T10:04:30.643838+00:00 app[web.1]: Bot [index.js] is ready!
2020-08-10T10:05:17.228802+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-08-10T10:05:17.246146+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-08-10T10:05:17.307500+00:00 heroku[web.1]: Process exited with status 137
2020-08-10T10:05:17.347445+00:00 heroku[web.1]: State changed from starting to crashed
the start command in the package.json is: node index.js
the Procfile is:
worker: node index.js
and index.js is:
const Discord = require("discord.js")
const client = new Discord.Client()
const welcomeMessage = require("./welcomeMessage")
client.on("ready", () => {
console.log("Bot [index.js] is ready!")
})
client.login(process.env.TOKEN)
I also have another file, welcomeMessage:
const Discord = require("discord.js")
const client = new Discord.Client()
client.on("ready", () => {
console.log("Bot [welcomeMessage.js] is ready!")
})
client.on('guildMemberAdd', member => {
let welcomeChannel = client.channels.cache.get('0000000000000')
welcomeChannel.send("hello world")
})
client.login(process.env.TOKEN)
What causes this peoblem? thx
Man, somehow I did here, first time ever a random try worked for me, I'm so happy! Look, I hope it will work for you too. First of all, go see your "package.json" file, inside it you must have the:
"scripts": {
},
You should put the "start" : "node index.js" there (even if there's already another things inside 'scripts', but don't delete the other stuff, just add this one) it will be like that:
"scripts": {
"start": "node index.js",
},
Then go to your Procfile and change the "worker : node index.js" to "worker npm start" (I tried it because I notice only web was working and the command of it was "web npm start")
It should solve the problem!
I'm pretty sure that it was what solved for me, now it's running non-stop, but if it's not what solved, I also used some commands on the Terminal, the commands: "heroku run worker", also "heroku ps:scale worker=1".
I hope it will help you, I was 8 hours straight trying to make it work because I had other errors, and I know how annoying is this thing not working.
Obs.: also you need to do what the other person said before:
"(...) you need to disable the web dyno and enable the worker dyno".
PS.: I know you already did the start thing, I'm just thinking if someone get here without this information maybe it will help.
Turns out that you need to disable the web dyno and enable the worker dyno.

Puppeteer discord bot keeps crashing when hosted on Heroku

Bot works fine locally and even in the simlulated Heroku local heroku local web, but crashes after a minute or two when hosted on their servers online.
I have installed the buildpack for puppeteer:
https://github.com/jontewks/puppeteer-heroku-buildpack
I cleared my build cache:
https://help.heroku.com/18PI5RSY/how-do-i-clear-the-build-cache
I have tried to manually set my worker scaling to 1 heroku ps:scale web=1
This is my puppeteer browser launch arguments: {args: ["--no-sandbox", "--disable-setuid-sandbox"],}
List of my dependencies below:
discord.js
dotenv
node-fetch
puppeteer
fs
Logs are useless:
2020-05-15T11:40:56.804117+00:00 heroku[web.1]: State changed from crashed to starting
2020-05-15T11:41:22.345051+00:00 app[web.1]:
2020-05-15T11:41:22.345073+00:00 app[web.1]: > discordbot#1.0.0 start /app
2020-05-15T11:41:22.345073+00:00 app[web.1]: > node bot.js
2020-05-15T11:41:22.345074+00:00 app[web.1]:
2020-05-15T11:42:20.278141+00:00 heroku[web.1]: State changed from starting to crashed
2020-05-15T11:53:02.553013+00:00 heroku[web.1]: State changed from crashed to starting
2020-05-15T11:53:25.758932+00:00 app[web.1]:
2020-05-15T11:53:25.758955+00:00 app[web.1]: > discordbot#1.0.0 start /app
2020-05-15T11:53:25.758956+00:00 app[web.1]: > node bot.js
2020-05-15T11:53:25.758956+00:00 app[web.1]:
2020-05-15T11:54:18.310840+00:00 heroku[web.1]: State changed from starting to crashed
I had a similar problem and applied this:
(Copied from Puppeteer Troubleshooting site. And It worked!)
Running Puppeteer on Heroku
Running Puppeteer on Heroku requires some additional dependencies that aren't included on the Linux box that Heroku spins up for you. To add the dependencies on deploy, add the Puppeteer Heroku buildpack to the list of buildpacks for your app under Settings > Buildpacks.
The url for the buildpack is https://github.com/jontewks/puppeteer-heroku-buildpack
Ensure that you're using '--no-sandbox' mode when launching Puppeteer. This can be done by passing it as an argument to your .launch() call: puppeteer.launch({ args: ['--no-sandbox'] });.
When you click add buildpack, simply paste that url into the input, and click save. On the next deploy, your app will also install the dependencies that Puppeteer needs to run.
If you need to render Chinese, Japanese, or Korean characters you may need to use a buildpack with additional font files like https://github.com/CoffeeAndCode/puppeteer-heroku-buildpack
There's also another simple guide from #timleland that includes a sample project: https://timleland.com/headless-chrome-on-heroku/.

Starting my Node.js + Mongo API with Heroku

I know it's maybe a beginner question, but I looked over it everywhere and I still having trouble with it.
I have a Node API that I run locally. The only thing I need to do is run in terminal:
$ mongod
and then
$ node src/loader.js
So, I upload my code to Heroku and added to my package.json:
"scripts": {
"start": "node src/loader.js"
}
But when I run '$ heroku ps' it says that my process crashed. Whats the difference between my local build and the Heroku build?
Adding my Heroku logs --tail.
2018-02-05T19:19:17.648257+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2018-02-05T19:19:17.648257+00:00 heroku[web.1]: Stopping process with SIGKILL
2018-02-05T19:19:17.732544+00:00 heroku[web.1]: Process exited with status 137
Start by having a look over the heroku logs from terminal 'heroku logs --tail', and if you need more detail you can go to the app in heroku and in the upper right view the logs in the heroku platform.
If you can post some of the logs it will be helpful in telling what's going wrong if you can't derive it from there on your review.
So, I discovered the problem. Heroku dynamically assigns your app a port, so you can't set the port to a fixed number, and in my code I assigned to port: '3000'.
So you need to change to
var port = process.env.PORT || 3000;

Node.js app works on foreman but not heroku. 404 Error

I'm using Yeoman/bower/grunt to build an angular app. The app runs perfectly on foreman. In my search of similar questions on stack most other people just forgot to create their Procfile or had mistakes in their app.js which ran the app. But I have both of those and I can't figure out what's wrong with them, such that foreman works and not heroku. When I deploy to heroku and open it I get this in the log:
2013-10-02T19:33:56.159151+00:00 heroku[api]: Deploy b417d22 by richardadavila#gmail.com
2013-10-02T19:33:56.202052+00:00 heroku[api]: Release v10 created by richardadavila#gmail.com
2013-10-02T20:38:51.350572+00:00 heroku[web.1]: Idling
2013-10-02T20:38:57.626159+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2013-10-02T20:39:02.097317+00:00 heroku[web.1]: State changed from up to down
2013-10-02T20:39:02.079612+00:00 heroku[web.1]: Process exited with status 143
2013-10-02T22:00:00.360769+00:00 heroku[web.1]: Unidling
2013-10-02T22:00:00.361135+00:00 heroku[web.1]: State changed from down to starting
2013-10-02T22:00:04.049439+00:00 heroku[web.1]: Starting process with command `node app.js`
2013-10-02T22:00:06.128657+00:00 app[web.1]: server started 40741
2013-10-02T22:00:06.164522+00:00 heroku[web.1]: State changed from starting to up
2013-10-02T22:00:07.479644+00:00 heroku[router]: at=info method=GET path=/ host=rickydavila-kpopbetter-s.herokuapp.com fwd="71.144.19.194" dyno=web.1 connect=9ms service=30ms status=404 bytes=22
No error (I probably don't fully understand the log), it just kills the app.
My Procfile says: "node app.js"
My app.js file that creates an express server to run the app is:
var express = require('express');
var path = require('path');
var app = express();
app.configure(function(){
'use strict';
app.use(express.bodyParser());
app.use(express.static(path.join(__dirname, 'dist')));
});
var port = process.env.PORT || 5000;
app.listen(port, function() {
'use strict';
console.log('server started '+port);
});
Grunt builds the app to my dist folder which I reference above. Any clear mistakes above? When I open the heroku app url, the page states: Cannot GET /
Oh I also have scaled the dynos as some other questions failed to do. And to my knowledge I'm not hardcoding the port.
Figured it out. Always check your .gitignore file! I forgot to remove dist from the file and it hadn't been committed or pushed to heroku.

Node.js port issue on Heroku cedar stack

I'm running a basic Express app in Node.js and trying to deploy to Heroku. The app works fine locally and I believe my setup with Heroku has gone well up until starting the server where i get the following error:
2011-09-21T16:42:36+00:00 heroku[web.1]: State changed from created to starting
2011-09-21T16:42:39+00:00 app[web.1]: Express server listening on port 3000 in production mode
2011-09-21T16:42:40+00:00 heroku[web.1]: Error R11 (Bad bind) -> Process bound to port 3000, should be 12810 (see environment variable PORT)
2011-09-21T16:42:40+00:00 heroku[web.1]: Stopping process with SIGKILL
2011-09-21T16:42:40+00:00 heroku[web.1]: Process exited
this is currently all i have in my app.js
app.listen(3000);
I did also run this as mentioned on Heroku's getting started.
$ heroku config:add NODE_ENV=production
Adding config vars:
NODE_ENV => production
I believe I just need to set up the port for production? Thanks.
Can you show the entire section of code where you call listen? You should be checking for the process environment variable PORT, not just hardcoding it to 3000. From their docs:
var port = process.env.PORT || 3000;
app.listen(port, function() {

Resources