Issue with React app and Nginx when deployed on Heroku - node.js

I have a React app created with 'create-react-app' and setup for Heroku deployment with " heroku create --buildpack https://github.com/mars/create-react-app-buildpack.git." The initial deployment and the following ones via Travis CI were successful. However, since yesterday, while the app is still being deployed after successful build, it can not be opened and the Heroku log reads:
2018-10-09T20:14:16.000000+00:00 app[api]: Build succeeded
2018-10-09T20:14:19.093204+00:00 heroku[web.1]: Starting process with command bin/boot
2018-10-09T20:14:21.599174+00:00 app[web.1]: Injecting runtime env into /app/build/static/js/1.65a45b50.chunk.js (from .profile.d/inject_react_app_env.sh)
2018-10-09T20:14:22.010832+00:00 app[web.1]: Injecting runtime env into /app/build/static/js/main.b9301262.chunk.js (from .profile.d/inject_react_app_env.sh)
2018-10-09T20:14:22.212043+00:00 app[web.1]: Injecting runtime env into /app/build/static/js/runtime~main.229c360f.js (from .profile.d/inject_react_app_env.sh)
2018-10-09T20:14:22.748085+00:00 app[web.1]: Starting log redirection...
2018-10-09T20:14:22.748475+00:00 app[web.1]: Starting nginx...
2018-10-09T20:14:22.846806+00:00 app[web.1]: Process exited unexpectedly: nginx
2018-10-09T20:14:22.847047+00:00 app[web.1]: Going down, terminating child processes...
2018-10-09T20:14:22.845551+00:00 app[web.1]: nginx: [emerg] invalid number of arguments in "set" directive in ./config/nginx.conf:76
2018-10-09T20:14:22.949293+00:00 heroku[web.1]: State changed from starting to crashed
2018-10-09T20:14:22.950957+00:00 heroku[web.1]: State changed from crashed to starting
2018-10-09T20:14:22.935763+00:00 heroku[web.1]: Process exited with status 1
I can't see what the "set" directive refers to and how I am supposed to configure it. From my understanding the way to configure the Nginx server's behavior would be via the static.json file (https://github.com/heroku/heroku-buildpack-static#configuration) but I might be wrong. So far it is configured this way:
{
"root": "build/",
"routes": {
"/**": "index.html"
},
"proxies": {
"/api/": {
"origin": "${PORT}"
}
}
}
I also added an app.json as indicated here https://github.com/mars/create-react-app-buildpack#user-content-commit--deploy-%EF%B8%8F :
{
"buildpacks": [
{
"url": "mars/create-react-app"
}
]
}
The buildpack I am using configures the dyno this way so I shouldn't need a Procfile
I have done lots of research but can't find anything that could solve this issue. Any help is much appreciated!
Thank you!

I removed the proxies from my static.json file and it fixed the issue. The PORT I was referring to was unnecessary as I have already setup CORS on my backend.

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/.

Heroku - bash: node: command not found. Process error code 127

I am struggling with the problem that I am not able to deploy my node application to the Heroku. Previously I have done it several times and everything was working like a charm. Now I encounter bash: node: command not found. I have no idea what to do to get rid of this problem.
My Procfile:
web: node ./src/index.js
My package.json:
{
"name": "scheduler_be",
"version": "1.0.0",
"description": "",
"main": "./src/index.js",
"scripts": {
"test": "jest --forceExit",
"start": "nodemon ./src/index.js --development"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jablonskipj/scheduler-be.git"
},
"author": "Patryk Jablonski",
"license": "ISC",
"bugs": {
"url": "https://github.com/jablonskipj/scheduler-be/issues"
},
"homepage": "https://github.com/jablonskipj/scheduler-be#readme",
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.16.4",
"jest": "^24.1.0",
"knex": "^0.16.5",
"node": "^12.2.0",
"node-cron": "^2.0.3",
"nodemon": "^1.18.9",
"npm": "^6.9.0",
"pg": "^7.8.0",
"pg-hstore": "^2.3.2",
"smsapi": "^1.5.7"
},
"engines": {
"node": "12.2.0"
}
}
and my index.js:
const express = require('express')
const reminderRoutes = require('./routes/Routes')
const bodyParser = require('body-parser')
const PORT = process.env.PORT || 8006
console.log(PORT)
const app = express()
app.use(bodyParser.json())
app.use('/reminder', reminderRoutes)
app.listen(PORT, () => {
console.log(`app listening on port ${PORT}`)
})
I have tried to log out anything valuable by using heroku logs -t but this does not give me anything more than Process error code 127.
Can anyone help me with this problem?
I have tried things suggested in the heroku node.js bash: node: command not found but nothing helped. That question was asked 7 years ago and never answered, so I am pretty sure that my problem is different here.
Is there anyone from the Heroku team that could help me with the problem? Recently I have tried to force the use of the npm in version 6.5.0 and this has no effect. I have read the whole https://devcenter.heroku.com/articles/troubleshooting-node-deploys and applied all of the suggestions. How can I force Heroku to give me more info about what is exactly the problem?
EDIT:
I have also tried without the Procfile and app still does not work. Now I receive bash: npm: command not found so I am getting pretty frustrated, because of that I have another Nodejs app deployed on the Heroku and it is deploying without a problem.
EDIT:
I have checked for multiple times and the buildset is heroku/nodejs. Is there anything else that I can do to deploy this app to the heroku somehow?
Additionally here is a screen of the logs that I can see in the Heroku console
The log file - text version:
2019-05-21T06:59:52.401049+00:00 heroku[web.1]: Starting process with command `node index.js`
2019-05-21T06:59:53.969667+00:00 heroku[web.1]: State changed from starting to crashed
2019-05-21T06:59:53.952550+00:00 heroku[web.1]: Process exited with status 127
2019-05-21T06:59:53.901105+00:00 app[web.1]: bash: node: command not found
2019-05-21T08:01:13.046896+00:00 app[api]: Starting process with command `node src/schedule.js` by user scheduler#addons.heroku.com
2019-05-21T08:01:18.939306+00:00 heroku[scheduler.8582]: Starting process with command `node src/schedule.js`
2019-05-21T08:01:19.672108+00:00 heroku[scheduler.8582]: State changed from starting to up
2019-05-21T08:01:20.475107+00:00 heroku[scheduler.8582]: State changed from up to complete
2019-05-21T08:01:20.454340+00:00 heroku[scheduler.8582]: Process exited with status 127
2019-05-21T08:01:20.379550+00:00 app[scheduler.8582]: bash: node: command not found
2019-05-21T08:21:17.000000+00:00 app[api]: Build started by user manilandiasalon#gmail.com
2019-05-21T08:22:22.166110+00:00 heroku[web.1]: State changed from crashed to starting
2019-05-21T08:22:27.607552+00:00 heroku[web.1]: Starting process with command `node index.js`
2019-05-21T08:22:29.106294+00:00 heroku[web.1]: State changed from starting to crashed
2019-05-21T08:22:29.182031+00:00 heroku[web.1]: State changed from crashed to starting
2019-05-21T08:22:29.086883+00:00 heroku[web.1]: Process exited with status 127
2019-05-21T08:22:28.981729+00:00 app[web.1]: bash: node: command not found
2019-05-21T08:22:20.927103+00:00 app[api]: Deploy d783e9a4 by user manilandiasalon#gmail.com
2019-05-21T08:22:20.927103+00:00 app[api]: Release v30 created by user manilandiasalon#gmail.com
2019-05-21T08:22:36.960377+00:00 heroku[web.1]: Starting process with command `node index.js`
2019-05-21T08:22:39.080765+00:00 heroku[web.1]: State changed from starting to crashed
2019-05-21T08:22:39.061234+00:00 heroku[web.1]: Process exited with status 127
2019-05-21T08:22:38.981195+00:00 app[web.1]: bash: node: command not found
2019-05-21T08:22:27.000000+00:00 app[api]: Build succeeded
After raising a ticket in the Heroku support page I received info that my application acts like a containerized app. In this case, I am not able to do anything about that and I have to wait till support team fix my issue
I cannot be sure as to what the problem might be that you are facing specifically but I can maybe provide you with some pointers.
Also I am hoping you have already checked out the generic solutions like checking if the official build pack is set or setting node engine's specified in package.json etc.
If all these doesn't work, could you try a heroku restart.
I am not familiar with heroku, but I can say that whenever you have a "bash: BLANK not found" error it usually means you are using the command from somewhere that does not know about the command.
So, in this case, node is not installed in a location that the current working directory knows about. Maybe you are telling heroku an incorrect location to look for node or something, or node is just not properly installed. Check where node is installed and if you need to tell heroku where it is installed then make sure the search paths are correct?

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;

Heroku Node app keeps crashing

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.

Resources