Facing NodeJS deploying error on Heroku, how can I fix it? - node.js

I deployed my NodeJS app on Heroku, But I'm getting this error:
Image of error
I've tried to deploy my NodeJs app on Heroku, follows all steps on Heroku guide but I can't make it running.
How can I fix it?
This is my app.js :
const express = require('express');
const app = express();
const path = require('path');
app.get('/', async (req, res) => {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.post('/hello', async (req, res) => {
// My code ....
});
app.listen(3000, () => console.log('listening on port 3000'));
This is my Package.json :
{
"name": "amer-api",
"version": "1.0.0",
"description": "Amer's personal website api.",
"author": "Amer Ansari",
"license": "ISC",
"homepage": "https://github.com/AmerAnsari/amer-api",
"repository": {
"type": "git",
"url": "git+https://github.com/AmerAnsari/amer-api.git"
},
"bugs": {
"url": "https://github.com/AmerAnsari/amer-api/issues"
},
"scripts": {
"start": "node app.js"
},
"main": "app.js",
"dependencies": {
"express": "^4.17.1",
"geoip-lite": "^1.4.2",
"nodemailer": "^6.4.6"
}
}
THere is the log (heroku logs --tail) :
amer#pire:~/Documents/GitHub/amer-api$ heroku logs --tail
2020-05-18T00:31:45.124394+00:00 app[api]: Initial release by user am3ransari#gmail.com
2020-05-18T00:31:45.124394+00:00 app[api]: Release v1 created by user am3ransari#gmail.com
2020-05-18T00:31:45.576958+00:00 app[api]: Enable Logplex by user am3ransari#gmail.com
2020-05-18T00:31:45.576958+00:00 app[api]: Release v2 created by user am3ransari#gmail.com
2020-05-18T00:32:22.000000+00:00 app[api]: Build started by user am3ransari#gmail.com
2020-05-18T00:32:49.104161+00:00 app[api]: Deploy 1d131d7d by user am3ransari#gmail.com
2020-05-18T00:32:49.104161+00:00 app[api]: Release v3 created by user am3ransari#gmail.com
2020-05-18T00:32:49.128088+00:00 app[api]: Scaled to web#1:Free by user am3ransari#gmail.com
2020-05-18T00:32:55.009626+00:00 app[web.1]:
2020-05-18T00:32:55.009659+00:00 app[web.1]: > amer-api#1.0.0 start /app
2020-05-18T00:32:55.009660+00:00 app[web.1]: > node app.js
2020-05-18T00:32:55.009660+00:00 app[web.1]:
2020-05-18T00:32:55.349511+00:00 app[web.1]: listening on port 3000
2020-05-18T00:32:58.000000+00:00 app[api]: Build succeeded
2020-05-18T00:33:53.578640+00:00 heroku[web.1]: State changed from starting to crashed
2020-05-18T00:33:53.583026+00:00 heroku[web.1]: State changed from crashed to starting
2020-05-18T00:34:01.570180+00:00 app[web.1]:
2020-05-18T00:34:01.570202+00:00 app[web.1]: > amer-api#1.0.0 start /app
2020-05-18T00:34:01.570202+00:00 app[web.1]: > node app.js
2020-05-18T00:34:01.570203+00:00 app[web.1]:
2020-05-18T00:34:02.125267+00:00 app[web.1]: listening on port 3000
2020-05-18T00:34:48.531089+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=serene-shelf-12092.herokuapp.com request_id=82d5065f-fcd6-4e9c-8836-7ebb3a075658 fwd="92.99.250.143" dyno= connect= service= status=503 bytes= protocol=https
2020-05-18T00:34:59.080489+00:00 heroku[web.1]: State changed from starting to crashed
2020-05-18T00:35:00.097273+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=serene-shelf-12092.herokuapp.com request_id=aa2cac26-b857-48aa-a2eb-72f9c9abea04 fwd="92.99.250.143" dyno= connect= service= status=503 bytes= protocol=https
Anyone can help me?

please add path into your dependencies:
npm install path --save
secondly change your app.js last line to this
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`listening on:`, port));

Related

Deploying Node.js app to Heroku with error H10 stating that "/tmp/start-d846bb8e.sh: 1: nodemon: not found"

So I've been trying to deploy my app with Heroku, and it runs locally perfectly fine, but I keep running into the same H10 error below:
2022-09-15T02:57:14.484472+00:00 heroku[web.1]: Starting process with command `npm start`
2022-09-15T02:57:17.725863+00:00 app[web.1]:
2022-09-15T02:57:17.725906+00:00 app[web.1]: > node-sso-example-app#1.0.0 start
2022-09-15T02:57:17.725912+00:00 app[web.1]: > nodemon index.js
2022-09-15T02:57:17.725915+00:00 app[web.1]:
2022-09-15T02:57:17.731558+00:00 app[web.1]: /tmp/start-d846bb8e.sh: 1: nodemon: not found
2022-09-15T02:57:17.847830+00:00 heroku[web.1]: Process exited with status 127
2022-09-15T02:57:17.916430+00:00 heroku[web.1]: State changed from starting to crashed
2022-09-15T02:57:19.230286+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=will-dersh.herokuapp.com request_id=9e07b48e-dcd7-42eb-b6bc-d9d7e138b15a fwd="108.67.29.121" dyno= connect= service= status=503 bytes= protocol=https
2022-09-15T02:57:19.499659+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=will-dersh.herokuapp.com request_id=f7501cb9-60c7-457d-97ed-63cffdfe872f fwd="108.67.29.121" dyno= connect= service= status=503 bytes= protocol=https
Here's my index.js file:
import express from 'express'
import 'dotenv/config'
import router from './routes/index.js'
import morgan from 'morgan'
const app = express()
app.use('/public', express.static('public'))
app.use(express.urlencoded({ extended: false }))
app.use(express.json())
app.use(morgan('dev'))
app.use('/', router)
app.listen(process.env.PORT || 8000, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});
Here's the package.json:
{
"name": "node-sso-example-app",
"version": "1.0.0",
"description": "Example Node.js SSO App using WorkOS",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js"
},
"author": "WorkOS",
"dependencies": {
"#workos-inc/node": "^2.11.0",
"cookie-parser": "^1.4.6",
"dotenv": "^16.0.1",
"ejs": "^3.1.8",
"express": "^4.18.1",
"express-session": "^1.17.2",
"http-errors": "~1.6.3",
"morgan": "^1.10.0",
"router": "^1.3.7"
},
"devDependencies": {
"nodemon": "^2.0.19"
}
}
And if it helps, here is my Procfile:
web:node index.js
I'm not really sure where I've gone wrong so any help would be appreciated!

MERN app works locally, but I get 503 when deploying to Heroku

I'm working on a MERN app, pretty much done, all I have left to do is deploy it. The app works perfectly when I run it locally, but I deploy to Heroku, I get the following error in the browser console:
frozen-basin-00083.herokuapp.com/:1 Failed to load resource: the server responded with a status of 503 (Service Unavailable)
/favicon.ico:1 Failed to load resource: the server responded with a status of 503 (Service Unavailable)
Heroku logs show the following:
2021-01-12T16:35:14.000000+00:00 app[api]: Build succeeded
2021-01-12T16:35:25.312880+00:00 heroku[web.1]: Starting process with command `npm start`
2021-01-12T16:35:30.220170+00:00 app[web.1]:
2021-01-12T16:35:30.220351+00:00 app[web.1]: > budget-app#1.0.0 start /app
2021-01-12T16:35:30.220352+00:00 app[web.1]: > node server.js
2021-01-12T16:35:30.220356+00:00 app[web.1]:
2021-01-12T16:35:32.450275+00:00 app[web.1]: Server is running on port 7834
2021-01-12T16:35:32.457602+00:00 app[web.1]: Error: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
2021-01-12T16:35:32.532983+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-01-12T16:35:32.533749+00:00 app[web.1]: npm ERR! errno 1
2021-01-12T16:35:32.553311+00:00 app[web.1]: npm ERR! budget-app#1.0.0 start: `node server.js`
2021-01-12T16:35:32.553624+00:00 app[web.1]: npm ERR! Exit status 1
2021-01-12T16:35:32.553937+00:00 app[web.1]: npm ERR!
2021-01-12T16:35:32.554191+00:00 app[web.1]: npm ERR! Failed at the budget-app#1.0.0 start script.
2021-01-12T16:35:32.554488+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-01-12T16:35:33.032396+00:00 app[web.1]:
2021-01-12T16:35:33.036236+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-01-12T16:35:33.036435+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-01-12T16_35_32_555Z-debug.log
2021-01-12T16:35:33.119008+00:00 heroku[web.1]: Process exited with status 1
2021-01-12T16:35:33.187983+00:00 heroku[web.1]: State changed from starting to crashed
2021-01-12T16:35:37.653703+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=frozen-basin-00083.herokuapp.com request_id=8403a766-1141-47b9-8fa3-79d77bbf5d01 fwd="208.104.192.108" dyno= connect= service= status=503 bytes= protocol=https
2021-01-12T16:35:39.026779+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=frozen-basin-00083.herokuapp.com request_id=50f48bcf-4534-450d-825d-1b9e91d7ad44 fwd="208.104.192.108" dyno= connect= service= status=503 bytes= protocol=https
2021-01-12T16:38:29.709828+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=frozen-basin-00083.herokuapp.com request_id=ca7f559f-2125-48b2-a22f-9f3180acb70b fwd="208.104.192.108" dyno= connect= service= status=503 bytes= protocol=https
2021-01-12T16:38:30.395524+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=frozen-basin-00083.herokuapp.com request_id=d0937812-09be-49df-8138-564ee7bbfce0 fwd="208.104.192.108" dyno= connect= service= status=503 bytes= protocol=https
2021-01-12T16:42:35.305633+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=frozen-basin-00083.herokuapp.com request_id=3d35bf8c-5c7f-441c-817a-6e15581d3bdc fwd="208.104.192.108" dyno= connect= service= status=503 bytes= protocol=https
2021-01-12T16:42:35.923358+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=frozen-basin-00083.herokuapp.com request_id=cd050371-fb57-49b5-93f0-05a928a5fe77 fwd="208.104.192.108" dyno= connect= service= status=503 bytes= protocol=https
Initially I thought it may be a problem with favicon because of the console error. At one point I tried to use my own icon so I deleted favicon and the HTML tag for it. I saw the error, added it back in and nothing changed. I also see there's a mongoose error in the heroku logs, but locally when I run npm start, it connects the db just fine. When I run the app locally everything is exactly how it should be, I'm only getting these problems when deploying.
Here is the package.json for the server:
{
"name": "budget-app",
"version": "1.0.0",
"description": "back end for budget tool",
"main": "server.js",
"scripts": {
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},
"author": "Evan Barton",
"license": "MIT",
"dependencies": {
"colors": "^1.4.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mongoose": "^5.11.11",
"morgan": "^1.10.0",
"node": "^15.4.0"
},
"devDependencies": {
"concurrently": "^5.3.0",
"nodemon": "^2.0.7"
}
}
Here is the package.json for the client:
{
"name": "budget-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.11.8",
"#testing-library/react": "^11.2.3",
"#testing-library/user-event": "^12.6.0",
"axios": "^0.21.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.1",
"shortid": "^2.2.16",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "http://localhost:5000"
}
Here is the server.js file:
require('dotenv').config()
const path = require('path')
const express = require('express')
const colors = require('colors')
const morgan = require('morgan')
const connectDB = require('./config/db-config')
connectDB()
const app = express()
app.use(express.json())
const transactionsRouter = require('./routes/transactions-router')
app.use('/api/transactions', transactionsRouter)
if(process.env.NODE_ENV === 'production'){
app.use(express.static('client/build'))
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
})
// app.use('*', express.static(path.join(__dirname, "client", "build")))
}
const PORT = process.env.PORT || 5000
app.get('/', (req, res) => res.send('Hello'))
app.listen(PORT, console.log(`Server is running on port ${PORT}`.blue.bold))
db-config:
const mongoose = require('mongoose')
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
})
console.log(`MongoDB connected: ${conn.connection.host}`.cyan.underline.bold)
} catch(err) {
console.log(`Error: ${err.message}`.red)
process.exit(1)
}
}
module.exports = connectDB
The issue was the environment variables. The reason it worked on my local machine was because of the env file, but when I deploy to Heroku, the env file is gitignored. So I had to set up the environment variables again in Heroku by going to setting > config vars, and configuring the variables the same way they are on my local machine. This fixed the problem instantly.

node.js app getting the R10 error on Heroku

I have previously looked into similar questions but non of the solutions worked out for me which is why I am asking this question. I am simply trying to deploy my node.js application to heroku but I keep getting an application error.
the following is a part of my logs:
2018-09-29T18:54:45.545079+00:00 app[web.1]: > pg_backend#1.0.0 start /app
2018-09-29T18:54:45.545081+00:00 app[web.1]: > node server.js
2018-09-29T18:54:45.545082+00:00 app[web.1]:
2018-09-29T18:54:46.035221+00:00 app[web.1]: listening to requests...
2018-09-29T18:55:43.134627+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2018-09-29T18:55:43.134859+00:00 heroku[web.1]: Stopping process with SIGKILL
2018-09-29T18:55:43.317846+00:00 heroku[web.1]: Process exited with status 137
2018-09-29T18:55:43.329780+00:00 heroku[web.1]: State changed from starting to crashed
2018-09-29T21:23:23.635112+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=project-gorilla-backend.herokuapp.com request_id=2ec25806-0c83-4a7d-9bd8-eab41a67e996 fwd="131.227.39.211" dyno= connect= service= status=503 bytes= protocol=https
2018-09-29T21:23:24.637219+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=project-gorilla-backend.herokuapp.com request_id=a0582179-52fe-4c55-ba87-b0c146d66962 fwd="131.227.39.211" dyno= connect= service= status=503 bytes= protocol=https
The following is my package.json file:
{
"name": "pg_backend",
"version": "1.0.0",
"description": "back end application for the project gorilla site",
"main": "index.js",
"scripts": {
"start": "node server.js"
},
"engines": {
"node": "8.10.0",
"npm": "5.6.0"
},
"author": "Christopher Salay",
"license": "ISC",
"dependencies": {
"express": "^4.16.3",
"nodemailer": "^4.6.8",
"socket.io": "^2.1.1",
"vimeo": "^2.1.0"
}
}
and this is my Procfile:
web: node server.js
the following is my server.js
let express = require('express'), app = express(), path = require('path'),
socket = require('socket.io'), emailModule = require('./email.js'),
formValidationModule = require('./formValidation.js'), vimeoModule = require('./vimeo.js'),
port = process.env.PORT || 5000;
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
let server = app.listen(port,'0.0.0.0',function(){
console.log('listening to requests...');
vimeoModule.search()
});
let io = socket(server);
io.on('connection', (socket)=>{
console.log('made a connection!');
socket.on('email', (data)=>{
if(formValidationModule.checkEmptyContact(data.client, data.email, data.name,
data.title, data.message)){
socket.emit('invalidData');
}
else {
/**
* * Here you need to set your email options which includes the clients email, destination email,
* subject and the text (the email content).
*/
emailModule.setMailOptions(data.email,/*'Info#project-gorilla.co.uk'*/'salay777#hotmail.co.uk'
, data.client + ' ' + '(' +
data.name + ')' + ' ' + data.title, data.message).then((mailOpts)=>{
emailModule.send(mailOpts)
});
console.log('email has been sent!');
}
});
});
link to the heroku app:
https://pg-gorilla-backend.herokuapp.com/
and I use the following command to deploy to heroku:
git push heroku master
You need to configure your port on heroku as you cannot pass static port to the app when deploying to heroku, Heroku has built in method to bind designated port to app which is deployed on its environment. You need to use env var to assign port to the app.
In heroku app you can do this by going through Settings > Show config vars > Add PORT 3000 in it and then heroku in built method will assign designated PORT number to the app.

Why my heroku node.js app is giving at=error code=H10 desc="App crashed" method=GET path="/"?

I am trying to run my simple node app on Heroku.
Here is the directory structure
├── app.js
├── assets
├── blog.html
├── index.html
├── node_modules
└── package.json
Here is my app.js
let express = require('express'),
path = require('path');
var app = express();
let server = require('http').Server(app);
app.use(express.static(path.join(__dirname)));
app.get('/', function(req, res, next){
res.sendStatus(200);
});
app.get('/blog.html', function(req, res,next){
res.sendFile(path.join(__dirname+"/blog.html"));
});
app.post('/contact', function(req, res, next){
});
server.listen('8000', function() {
console.log("App is running on port 8000");
});
Here is the package.json
{
"name": "website",
"version": "1.0.0",
"engines" : {
"node" : "6.3.1",
"npm" : "3.10.3"
},
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start" : "node app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.14.0"
}
}
When I go to the console it rightly prints app is starting at xxxx port.
But then the app crashes with the following message
2016-08-10T13:12:49.839138+00:00 app[web.1]: App is running on port xxxx
2016-08-10T13:13:34.944963+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=saras-website.herokuapp.com request_id=28d8705a-d5a4-4aaa-bd8d-4c4c6101fbd4 fwd="106.51.20.181" dyno= connect= service= status=503 bytes=
2016-08-10T13:13:48.295315+00:00 heroku[web.1]: State changed from starting to crashed
2016-08-10T13:13:48.552740+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=saras-website.herokuapp.com request_id=b77e151f-7017-482d-b4ba-15d980534fd7 fwd="106.51.20.181" dyno= connect= service= status=503 bytes=
2016-08-10T13:13:50.163466+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=saras-website.herokuapp.com request_id=1e7b57e5-1056-4cb3-b41f-cd3f11794efe fwd="106.51.20.181" dyno= connect= service= status=503 bytes=
I don't know what am I doing wrong here... Help is appreciated
Set port like this
ES5:
var port = process.env.PORT || 8000;
And
server.listen(port, function() {
console.log("App is running on port " + port);
});
ES6:
const port = process.env.PORT || 8000;
And
server.listen(port, () => {
console.log("App is running on port " + port);
});
This allows Heroku to set the port at run time.

Node.js with express app crashes on Heroku, works locally

I have a Node.js with express running fine locally but crashes when I try to run it on Heroku.
When I deploy and go to the heroku subdomain:
The HTML loads but the CSS doesn't
When I refresh nothing loads and I get the generic application error
Looking at the logs it seems the app has crashed
Any ideas why heroku keeps crashing? Best guess is something to do with my static files.
Here are the logs:
2014-07-19T02:02:00.723361+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/javascripts/main.js" host=sendmyemail.herokuapp.com request_id=3f2d4cd8-c70d-4506-9baf-af8d98983f37 fwd="99.43.254.71" dyno=web.1 connect=1 service=43 status=503 bytes=691
2014-07-19T02:02:00.708143+00:00 app[web.1]: throw er; // Unhandled 'error' event
2014-07-19T02:02:00.709158+00:00 app[web.1]: at errnoException (child_process.js:988:11)
2014-07-19T02:02:00.708150+00:00 app[web.1]: ^
2014-07-19T02:02:00.707641+00:00 app[web.1]:
2014-07-19T02:02:00.707753+00:00 app[web.1]: events.js:72
2014-07-19T02:02:00.709155+00:00 app[web.1]: Error: spawn ENOENT
2014-07-19T02:02:00.709160+00:00 app[web.1]: at Process.ChildProcess._handle.onexit (child_process.js:779:34)
2014-07-19T02:02:02.265464+00:00 heroku[web.1]: State changed from up to crashed
2014-07-19T02:02:03.554266+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=sendmyemail.herokuapp.com request_id=fc319b3f-924f-4e37-bf6f-a58a4dc25770 fwd="99.43.254.71" dyno=web.1 connect=100 service= status=503 bytes=
2014-07-19T02:02:02.255985+00:00 heroku[web.1]: Process exited with status 8
Here is my package.json:
{
"name": "myapp",
"version": "0.0.1",
"engines": {
"node": "0.10.26",
"npm": "1.4.20"
},
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "~4.2.0",
"body-parser": "^1.4.3",
"node-compass": "0.2.3",
"ejs": "~1.0.0",
"express-ejs-layouts": "~1.1.0",
"mailgun-js": "^0.5.1"
}
}
I have my css and js in public folder and have this line in app.js:
app.use(express['static'](path.join(__dirname, 'public')));
I moved the line of code referring to static files (express.static) up above other lines of code and now it works fine on Heroku.
app.use(express.static(__dirname + '/public'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('layout', 'layout'); // defaults to 'layout'
app.use(require('node-compass')({mode: 'expanded'}));
app.use(expressLayouts);
app.use(bodyParser.urlencoded({ extended: false }));

Resources