Unable to deploy Contact Page Backend in Node.js to Heroku - node.js

I wrote backend in Node.js for the contact page on my portfoilio on Firebase. I'm trying to deploy it, but when open the app, it gives me an 'Application Error'. When I go to the logs, it gives me error code=H10 desc="App crashed".
Update: I also see an error Error: Cannot find module '#sendGrid/mail'.
I've tried a few things. I added "start": "node App.js" and "engines": { "node": "12.13.1" } to my package.json. I created a Procfile with web: node App.js. In my App.js, I changed my app.listen(4000, '0.0.0.0'); to app.listen(process.env.PORT || 4000);.
I'm not sure if I have to set process.env.PORT to something. How would I fix this?
Relevant Code
App.js
const express = require('express'); //Needed to launch server.
const bodyParser = require('body-parser');
const cors = require('cors'); //Needed to disable sendgrid security.
const sendGrid = require('#sendGrid/mail'); //Access SendGrid library to send emails.
sendGrid.setApiKey(process.env.SENDGRID_API_KEY);
const app = express(); //Alias from the express function.
app.use(bodyParser.json());
app.use(cors());
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
app.get('/api', (req, res, next) => {
res.send('API Status: Running');
});
app.post('/api/email', (req, res, next) => {
console.log(req.body);
const msg = {
to: 'my#email.com',
from: req.body.email,
subject: req.body.subject,
text: req.body.message
}
sendGrid.send(msg)
.then(result => {
res.status(200).json({
success: true
});
})
.catch(err => {
console.log('error: ', err);
res.status(401).json({
success: false
});
});
});
app.listen(process.env.PORT || 4000);
Also, here's my package.json
{
"name": "my-app-api",
"version": "1.0.0",
"description": "",
"main": "App.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node App.js"
},
"author": "Daniel Zhang",
"license": "ISC",
"dependencies": {
"#sendgrid/mail": "^7.2.2",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"nodemon": "^2.0.4"
},
"engines": {
"node": "12.13.1"
},
"devDependencies": {}
}
Also, here are the Application Logs:
2020-08-30T14:49:33.088197+00:00 heroku[web.1]: Starting process with command `node index.js`
2020-08-30T14:49:35.380821+00:00 heroku[web.1]: Process exited with status 1
2020-08-30T14:49:35.420886+00:00 heroku[web.1]: State changed from starting to crashed
2020-08-30T14:49:35.328408+00:00 app[web.1]: internal/modules/cjs/loader.js:800
2020-08-30T14:49:35.328435+00:00 app[web.1]: throw err;
2020-08-30T14:49:35.328436+00:00 app[web.1]: ^
2020-08-30T14:49:35.328436+00:00 app[web.1]:
2020-08-30T14:49:35.328437+00:00 app[web.1]: Error: Cannot find module '#sendGrid/mail'
2020-08-30T14:49:35.328437+00:00 app[web.1]: Require stack:
2020-08-30T14:49:35.328437+00:00 app[web.1]: - /app/index.js
2020-08-30T14:49:35.328438+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:797:15)
2020-08-30T14:49:35.328438+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:690:27)
2020-08-30T14:49:35.328439+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:852:19)
2020-08-30T14:49:35.328439+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:74:18)
2020-08-30T14:49:35.328439+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:4:18)
2020-08-30T14:49:35.328440+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:959:30)
2020-08-30T14:49:35.328440+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
2020-08-30T14:49:35.328440+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:815:32)
2020-08-30T14:49:35.328441+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:727:14)
2020-08-30T14:49:35.328441+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10) {
2020-08-30T14:49:35.328441+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2020-08-30T14:49:35.328442+00:00 app[web.1]: requireStack: [ '/app/index.js' ]
2020-08-30T14:49:35.328442+00:00 app[web.1]: }
2020-08-30T14:49:46.278674+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/api" host=daniel-zhang-portfolio-backend.herokuapp.com request_id=b50170d2-6e1f-4697-aa35-3ea445d1d936 fwd="75.75.104.235" dyno= connect= service= status=503 bytes= protocol=https
2020-08-30T14:49:46.490432+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=daniel-zhang-portfolio-backend.herokuapp.com request_id=cb1bdef2-0cb6-400d-aaf5-e8e2c2b8fa35 fwd="75.75.104.235" dyno= connect= service= status=503 bytes= protocol=https
Also here's my file structure:

In const sendGrid = require('#sendGrid/mail');, '#sendGrid/mail' should be '#sendgrid/mail'.

I'm posting this answer because I was getting the same error message. However, the issue was not related to the case import/require statement.
The error i was facing was following (Error: Cannot find module '#sendgrid/mail')
However, my application was running locally without any errors, as #sendgrid/mail was present inside my node_modules folder.
So what was the culprit?
Thw issue was inside my package.json file, as it was not present inside it as a dependency.
Solution
I ran the following commands inside my Vs code terminal.
npm install #sendgrid/mail
git add .
git commit -m "my fix"
git push heroku master
Note: you may update the push command according to your branch, in my case it was master.

Related

My app deployed successfully in Heroku but in times of opening the app show error

My app successfully deployed to Heroku..
****: Deployed 6bc02eb8 Today at 4:19 PM · v23 ·
Compare diff **** ****: Build
succeeded Today at 4:18 PM · View build log
But It shows error when try to open my app...
2022-06-29T10:19:45.228279+00:00 app[web.1]: npm ERR!
2022-06-29T10:19:45.228377+00:00 app[web.1]: npm ERR! Failed at the
blog-application#1.0.0 start script. 2022-06-29T10:19:45.228470+00:00
app[web.1]: npm ERR! This is probably not a problem with npm. There is
likely additional logging output above.
2022-06-29T10:19:45.236802+00:00 app[web.1]:
2022-06-29T10:19:45.237020+00:00 app[web.1]: npm ERR! A complete log
of this run can be found in: 2022-06-29T10:19:45.237112+00:00
app[web.1]: npm ERR!
/app/.npm/_logs/2022-06-29T10_19_45_229Z-debug.log
2022-06-29T10:19:45.431649+00:00 heroku[web.1]: Process exited with
status 1 2022-06-29T10:19:45.541882+00:00 heroku[web.1]: State changed
from starting to crashed 2022-06-29T10:19:48.705353+00:00
heroku[router]: at=error code=H10 desc="App crashed" method=GET
path="/" host=syt-share-your-thoughts.herokuapp.com
request_id=0bb2aaff-6bce-41fb-891e-d3b361a122f8 fwd="59.153.103.22"
dyno= connect= service= status=503 bytes= protocol=https
2022-06-29T10:19:51.151462+00:00 heroku[router]: at=error code=H10
desc="App crashed" method=GET path="/favicon.ico"
host=syt-share-your-thoughts.herokuapp.com
request_id=c0d24108-01a7-44b7-96dd-e8cc62bcaa7c fwd="59.153.103.22"
dyno= connect= service= status=503 bytes= protocol=https
here is my package.json file:
{
"name": "blog-application",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js",
"dev": "nodemon app.js"
},
"author": "Md Shareful Islam",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.0.1",
"chalk": "^4.1.0",
"cheerio": "^1.0.0-rc.11",
"config": "^3.3.7",
"connect-flash": "^0.1.1",
"connect-mongodb-session": "^3.1.1",
"debug": "^4.3.4",
"dotenv": "^16.0.1",
"ejs": "^3.1.8",
"express": "^4.18.1",
"express-session": "^1.17.3",
"express-validator": "^6.14.1",
"moment": "^2.29.3",
"mongoose": "^6.3.8",
"morgan": "^1.10.0",
"multer": "^1.4.5-lts.1",
"nodemon": "^2.0.16",
"reading-time": "^1.5.0",
"validator": "^13.7.0"
},
"engines": {
"node": "14.15.5"
}
}
And this is my main file:
require('dotenv').config()
const express = require('express');
const mongoose = require('mongoose')
const config = require('config')
const chalk = require('chalk')
// import middleware
const setMiddleware = require('./middleware/middleware')
//Import Routes
const setRoutes = require('./routes/routes')
const MONGODB_URI = `mongodb+srv://${config.get('db-admin')}:${config.get('db-password')}#blogapp.ool50gr.mongodb.net/?retryWrites=true&w=majority`
const app = express();
// Setup View Engine
app.set('view engine', 'ejs')
app.set('views', 'views')
// Using Middleware from Middleware Directory
setMiddleware(app)
// Using Routes from Route Directory
setRoutes(app)
// ** middleware to handle 404 and 500.
app.use((req, res, next) => {
let error = new Error('404 Page Not Found');
error.status = 404;
next(error);
})
app.use((error, req, res, next) => {
if(error.status === 404) {
return res.render('pages/error/404', {flashMessage: {}})
}
res.render('pages/error/500', {flashMessage: {}})
})
const PORT = process.env.PORT || 8080;
mongoose.connect(MONGODB_URI,
{useNewUrlParser: true})
.then(() => {
console.log(chalk.green.bold('Database connected!'))
app.listen(PORT, () => {
console.log(chalk.green.italic(`App is running on PORT ${PORT}`));
});
})
.catch(e => {
return console.log(e)
})
Here are logs datails from Heroku...
2022-06-29T16:18:56.404997+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2022-06-29T16:18:56.405076+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2022-06-29T16_18_56_398Z-debug.log
2022-06-29T16:18:56.554135+00:00 heroku[web.1]: Process exited with status 1
2022-06-29T16:18:56.637092+00:00 heroku[web.1]: State changed from starting to crashed
2022-06-29T16:18:56.640222+00:00 heroku[web.1]: State changed from crashed to starting
2022-06-29T16:19:00.220649+00:00 heroku[web.1]: Starting process with command `npm start`
2022-06-29T16:19:03.473783+00:00 app[web.1]:
2022-06-29T16:19:03.473792+00:00 app[web.1]: > blog-application#1.0.0 start /app
2022-06-29T16:19:03.473792+00:00 app[web.1]: > node app.js
2022-06-29T16:19:03.473792+00:00 app[web.1]:
2022-06-29T16:19:35.090006+00:00 app[web.1]: /app/node_modules/mongodb/lib/utils.js:417
2022-06-29T16:19:35.090017+00:00 app[web.1]: throw error;
2022-06-29T16:19:35.090018+00:00 app[web.1]: ^
2022-06-29T16:19:35.090019+00:00 app[web.1]:
2022-06-29T16:19:35.090020+00:00 app[web.1]: Error: Error connecting to db: connection <monitor> to 13.250.175.230:27017 closed
2022-06-29T16:19:35.090021+00:00 app[web.1]: at /app/node_modules/connect-mongodb-session/index.js:88:17
2022-06-29T16:19:35.090021+00:00 app[web.1]: at /app/node_modules/mongodb/lib/utils.js:413:17
2022-06-29T16:19:35.090022+00:00 app[web.1]: at /app/node_modules/mongodb/lib/mongo_client.js:129:28
2022-06-29T16:19:35.090022+00:00 app[web.1]: at connectCallback (/app/node_modules/mongodb/lib/operations/connect.js:29:9)
2022-06-29T16:19:35.090023+00:00 app[web.1]: at /app/node_modules/mongodb/lib/operations/connect.js:78:20
2022-06-29T16:19:35.090023+00:00 app[web.1]: at Object.callback (/app/node_modules/mongodb/lib/sdam/topology.js:208:50)
2022-06-29T16:19:35.090024+00:00 app[web.1]: at Timeout._onTimeout (/app/node_modules/mongodb/lib/sdam/topology.js:319:33)
2022-06-29T16:19:35.090024+00:00 app[web.1]: at listOnTimeout (internal/timers.js:554:17)
2022-06-29T16:19:35.090024+00:00 app[web.1]: at processTimers (internal/timers.js:497:7)
2022-06-29T16:19:35.109275+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2022-06-29T16:19:35.109568+00:00 app[web.1]: npm ERR! errno 1
2022-06-29T16:19:35.113501+00:00 app[web.1]: npm ERR! blog-application#1.0.0 start: `node app.js`
2022-06-29T16:19:35.113599+00:00 app[web.1]: npm ERR! Exit status 1
2022-06-29T16:19:35.113705+00:00 app[web.1]: npm ERR!
2022-06-29T16:19:35.113792+00:00 app[web.1]: npm ERR! Failed at the blog-application#1.0.0 start script.
2022-06-29T16:19:35.113876+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2022-06-29T16:19:35.120692+00:00 app[web.1]:
2022-06-29T16:19:35.120808+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2022-06-29T16:19:35.120859+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2022-06-29T16_19_35_114Z-debug.log
2022-06-29T16:19:35.785417+00:00 heroku[web.1]: State changed from starting to crashed
2022-06-29T16:19:35.273101+00:00 heroku[web.1]: Process exited with status 1
2022-06-29T16:19:38.894464+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=syt-share-your-thoughts.herokuapp.com request_id=2b13bcd5-e764-4e60-a254-394a8b0f7531 fwd="59.153.103.22" dyno= connect= service= status=503 bytes= protocol=https
2022-06-29T16:19:41.185482+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=syt-share-your-thoughts.herokuapp.com request_id=6109b89a-818d-4827-804b-98c1a875a3ab fwd="59.153.103.22" dyno= connect= service= status=503 bytes= protocol=https
you don't provide enought information for us to be able to help you, nobody can or will debug it for you in that regard.
Here, sharing your package.json and the entry file of your application would be useful!
Anyway it seems that npm run start is crashing, you could start by making sure that whatever command is run by npm run start is accessible in heroku (You might use a global dependencies that you have installed on your local machine which heroku doesn't).
I'll be glad to help you more if you provide required code snippets for us to understand what's happening!

Everything works on Heroku local but have 503 error on Heroku live

This is my first time using Heroku to deploy app and I had done a lot of research, I had tried all the ways that seem to make it work but still no luck. Of course, everything works on development mode (localhost), so I try to deploy it. I successfully get everything to work on Heroku local, then I try to publish to Heroku live, but it gives me "Failed to load resource: the server responded with a status of 503 (Service Unavailable)" error. But people say the Heroku local is like if you have deployed the app online, you see what you will see after you have deployed it. I really don't know what's wrong
One thing I am not sure about is, whether my folder structure is too complicated.
This
is my folder structure, the root folder with package-lock.json, package.json, Procfile, and a sub-folder containing the frontend and backend folder.
But it is working on Heroku local, so even the folder structure is a bit complicated, but it is still pointing to the right location to function???
Profile:
web: npm start
Package.json in root folder:
"name": "comic-bookstore",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "cd comic-book-store/backend/ && node server.js",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false cd comic-book-store/frontend/ && npm install && npm run build",
"backend": "cd comic-book-store/backend/ && nodemon server.js",
"frontend": "cd comic-book-store/frontend/ && npm start",
"test": "echo \"Error: no test specified\" && exit 1"
}
Package.json in frontend folder:
"name": "frontend",
"version": "0.1.0",
"private": true,
"proxy": "https://localhost:3001",
"dependencies": {
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"axios": "^0.21.1",
"bootstrap": "^5.1.0",
"env-cmd": "^10.1.0",
"jwt-decode": "^3.1.2",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-router-dom": "^5.3.0",
"react-router-redux": "^5.0.0-alpha.9",
"react-scripts": "4.0.3",
"serve": "^13.0.2",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
Package.json in backend folder:
"name": "backend",
"version": "1.0.0",
"engines": {
"node": "16.15.0",
"npm": "8.5.5"
},
"description": "",
"main": "server.js",
"homepage": ".",
"dependencies": {
"bcrypt": "^5.0.1",
"body-parser": "^1.19.0",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"express-session": "^1.17.2",
"express-validator": "^6.12.1",
"jsonwebtoken": "^8.5.1",
"mongodb": "^4.1.0",
"mongoose": "^5.13.7",
"multer": "^1.4.3",
"nodemon": "^2.0.13",
"webpack": "^5.72.1"
},
"devDependencies": {
"#babel/core": "^7.15.0",
"#babel/node": "^7.x",
"#babel/plugin-proposal-class-properties": "^7.14.5",
"#babel/preset-env": "^7.15.0",
"#babel/preset-react": "^7.14.5",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.2.2",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.24.0",
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-react": "^7.24.0",
"prettier": "^2.3.2"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
}
server.js:
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
// const session = require('express-session');
const cookieParser = require('cookie-parser');
const memberRoute = require('./routes/memberRoute');
const bookRoute = require('./routes/bookRoute');
const bookReservedRoute = require('./routes/bookReservedRoute');
const app = express();
const port = process.env.PORT || 3001;
const host = '0.0.0.0';
dotenv.config();
mongoose.connect(
process.env.DATABASE_ACCESS,
{
useNewUrlParser: true,
useUnifiedTopology: true,
},
() => console.log('Database connected')
);
app.use(express.json());
app.use(cors());
app.use('/app', memberRoute);
app.use('/app', bookRoute);
app.use('/app', bookReservedRoute);
app.use('/uploads', express.static('uploads'));
if(process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, '../frontend/build')));
app.get('/^((?!(app)).)*$/', (req, res) => {
res.sendFile(path.join(__dirname, '../frontend', 'build', 'index.html'))
})
}
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser());
app.listen(port, host, () => {
console.log(`Express server listening on port ${port} and host ${host}!`);
});
env file in backend folder:
DATABASE_ACCESS = mongodb+srv://<my-database-credential>#comic-bookstore.4zots.mongodb.net/Member?retryWrites=true&w=majority
JWT_SECRET = HJ2H2WUHJ2CHghwhh3hjndsndhje3ujjwquyhnshwy22
NODE_ENV = production
I had copied the above config var to Heroku dashboard as well.
heroku logs --tail
2022-05-31T07:27:37.302703+00:00 heroku[web.1]: Process exited with status 1
2022-05-31T07:27:37.397210+00:00 heroku[web.1]: State changed from starting to crashed
2022-05-31T07:27:37.427713+00:00 heroku[web.1]: State changed from crashed to starting
2022-05-31T07:27:45.314929+00:00 heroku[web.1]: Starting process with command `npm start`
2022-05-31T07:27:46.948920+00:00 app[web.1]:
2022-05-31T07:27:46.948931+00:00 app[web.1]: > comic-bookstore#1.0.0 start
2022-05-31T07:27:46.948932+00:00 app[web.1]: > cd comic-book-store/backend/ && node server.js
2022-05-31T07:27:46.948932+00:00 app[web.1]:
2022-05-31T07:27:47.337366+00:00 app[web.1]: node:internal/modules/cjs/loader:1189
2022-05-31T07:27:47.337376+00:00 app[web.1]: return process.dlopen(module, path.toNamespacedPath(filename));
2022-05-31T07:27:47.337376+00:00 app[web.1]: ^
2022-05-31T07:27:47.337377+00:00 app[web.1]:
2022-05-31T07:27:47.337378+00:00 app[web.1]: Error: /app/comic-book-store/backend/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node: invalid ELF header
2022-05-31T07:27:47.337378+00:00 app[web.1]: at Object.Module._extensions..node (node:internal/modules/cjs/loader:1189:18)
2022-05-31T07:27:47.337379+00:00 app[web.1]: at Module.load (node:internal/modules/cjs/loader:981:32)
2022-05-31T07:27:47.337379+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-05-31T07:27:47.337379+00:00 app[web.1]: at Module.require (node:internal/modules/cjs/loader:1005:19)
2022-05-31T07:27:47.337380+00:00 app[web.1]: at require (node:internal/modules/cjs/helpers:102:18)
2022-05-31T07:27:47.337380+00:00 app[web.1]: at Object.<anonymous> (/app/comic-book-store/backend/node_modules/bcrypt/bcrypt.js:6:16)
2022-05-31T07:27:47.337380+00:00 app[web.1]: at Module._compile (node:internal/modules/cjs/loader:1105:14)
2022-05-31T07:27:47.337380+00:00 app[web.1]: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
2022-05-31T07:27:47.337381+00:00 app[web.1]: at Module.load (node:internal/modules/cjs/loader:981:32)
2022-05-31T07:27:47.337381+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-05-31T07:27:47.337381+00:00 app[web.1]: at Module.require (node:internal/modules/cjs/loader:1005:19)
2022-05-31T07:27:47.337381+00:00 app[web.1]: at require (node:internal/modules/cjs/helpers:102:18)
2022-05-31T07:27:47.337382+00:00 app[web.1]: at Object.<anonymous> (/app/comic-book-store/backend/routes/memberRoute.js:5:16)
2022-05-31T07:27:47.337382+00:00 app[web.1]: at Module._compile (node:internal/modules/cjs/loader:1105:14)
2022-05-31T07:27:47.337382+00:00 app[web.1]: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
2022-05-31T07:27:47.337382+00:00 app[web.1]: at Module.load (node:internal/modules/cjs/loader:981:32) {
2022-05-31T07:27:47.337383+00:00 app[web.1]: code: 'ERR_DLOPEN_FAILED'
2022-05-31T07:27:47.337383+00:00 app[web.1]: }
2022-05-31T07:27:47.491907+00:00 heroku[web.1]: Process exited with status 1
2022-05-31T07:27:47.544210+00:00 heroku[web.1]: State changed from starting to crashed
2022-05-31T07:27:49.659165+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=sakura-comic-bookstore.herokuapp.com request_id=46ff615e-9342-4840-9138-8a6f99af5d6e fwd="151.210.168.207" dyno= connect= service= status=503 bytes= protocol=https
2022-05-31T07:27:50.267049+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=sakura-comic-bookstore.herokuapp.com request_id=c6af9a12-05e7-4deb-85dc-1e9fec2933c6 fwd="151.210.168.207" dyno= connect= service= status=503 bytes= protocol=https
2022-05-31T07:28:10.739605+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=sakura-comic-bookstore.herokuapp.com request_id=39a85204-136c-41e8-989a-ece16cabc887 fwd="151.210.168.207" dyno= connect= service= status=503 bytes= protocol=https
2022-05-31T07:28:11.279361+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=sakura-comic-bookstore.herokuapp.com request_id=44901387-4b2b-42a4-a15a-7747bd663d52 fwd="151.210.168.207" dyno= connect= service= status=503 bytes= protocol=https
I did not use the MongoDB sandbox in Heroku, I just connect my project database in Mongo Atlas when I developing the project. I think my database has no problem connecting with the app, I am able to see the data in Heroku local, the CRUD operations work fine in Heroku local.
Another thing I am not sure is, at the beginning of creating this project, I attempted to configure the React, Webpack, and Babel, but my tutor told me just use the React boilerplate, so I remove the config file of the Webpack, but I think I have not completely removed the Webpack and Babel dependencies in the frontend and backend folders, I am not sure if that affect the deployment in Heroku.
I really want to get the deployment done, and I think I had tries a lot of ways that seems to work, please give me a hint of what to do. I have no idea why everything work on Heroku local but not the Heroku live!!
My code doesn't work when I deploy it to heroku
This seems to cover the same issue, try to delete node_modules folders, and uninstall and install bcrypt from the backend

Node & Heroku: Fails at start script, but works perfectly on my local machine

I am working on deploying Net Ninja's JWT authentication tutorial/project to heroku, but I am getting an error that I have not been able to overcome. This application works perfectly on my local computer. So far I've read through several stack overflow questions, but every fix for this issue that I've come across, I have already implemented.
Here are the heroku logs of my error.
2020-12-03T16:22:13.292858+00:00 app[web.1]: npm ERR!
2020-12-03T16:22:13.292982+00:00 app[web.1]: npm ERR! Failed at the node-express-jwt-auth#1.0.0 start script.
2020-12-03T16:22:13.293125+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-12-03T16:22:13.300898+00:00 app[web.1]:
2020-12-03T16:22:13.301049+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-12-03T16:22:13.301127+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-12-03T16_22_13_293Z-debug.log
2020-12-03T16:22:13.432936+00:00 heroku[web.1]: Process exited with status 1
2020-12-03T16:22:13.479796+00:00 heroku[web.1]: State changed from starting to crashed
2020-12-03T18:29:03.230278+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=net-ninja-jwt-tutorial.herokuapp.com request_id=b693f181-6061-4704-ad14-08b71b6b0cb0 fwd="208.102.105.218" dyno= connect= service= status=503 bytes= protocol=https
2020-12-03T18:29:04.077018+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=net-ninja-jwt-tutorial.herokuapp.com request_id=d363fc10-2e07-4e50-a024-c9f848b19c42 fwd="208.102.105.218" dyno= connect= service= status=503 bytes= protocol=https
My package.json is as follows:
{
"name": "node-express-jwt-auth",
"version": "1.0.0",
"description": "",
"main": "app.js",
"dependencies": {
"bcrypt": "^5.0.0",
"dotenv": "^8.2.0",
"ejs": "^3.1.3",
"express": "^4.17.1",
"mongoose": "^5.9.23",
"validator": "^13.1.17"
},
"devDependencies": {},
"scripts": {
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/iamshaunjp/node-express-jwt-auth.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/iamshaunjp/node-express-jwt-auth/issues"
},
"engine":{
"node":"12.x"
}
}
My app.js file reads as follows:
const express = require('express');
const mongoose = require('mongoose');
require('dotenv').config()
// import cookie parser
const cookieParser = require('cookie-parser');
// import require auth
const {requireAuth, checkUser} = require('./middleware/authMiddleware')
// import authRoutes
const authRoutes = require('./routes/authRoutes')
const app = express();
const port = process.env.PORT || 3000
// middleware
app.use(express.static('public'));
// below allows us to use JSON in our requests
app.use(express.json());
// allows us to use cookieParser as middleware
app.use(cookieParser());
// view engine
app.set('view engine', 'ejs');
// database connection
const dbURI = 'mongodb+srv://david:Looping!#nodeauth.uj5ph.mongodb.net/<dbname>?retryWrites=true&w=majority';
mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex:true })
.then((result) => {
app.listen(port)
console.log(`listening on port ${port}`)
})
.catch((err) => console.log(err));
// routes
//
app.get('*', checkUser);
app.get('/', (req, res) => res.render('home'));
app.get('/smoothies', requireAuth, (req, res) => res.render('smoothies'));
// allow us to import are authroutes into the app.js file.
app.use(authRoutes)
// cookies routes
// app.get('/set-cookies', (req, res) =>{
// // res.setHeader('Set-Cookie','newUser=true')
// // npm install cookie-parser
// res.cookie("newUser", false);
// res.cookie("isEmployee", true, {maxAge:1000*60*60*24, httpOnly:true});
// res.send('you got the cookies!');
// });
// app.get('/read-cookies', (req, res) =>{
// const cookies = req.cookies;
// console.log(cookies)
// res.json(cookies)
// });
Lastly, my procfile reads:
web:node app.js
If anyone were able to help me out with this, it would be a very big help.
On a side note, when my heroku logs read:
2020-12-03T16:22:13.301049+00:00 app[web.1]: npm ERR! A complete log
of this run can be found in: 2020-12-03T16:22:13.301127+00:00
app[web.1]: npm ERR!
/app/.npm/_logs/2020-12-03T16_22_13_293Z-debug.log
How would I go about accessing those error logs?
Edit: after running
$heroku logs --app net-ninja-jwt-tutorial
I disovered the following error:
2020-12-03T19:50:24.064213+00:00 app[web.1]: Error: Cannot find module 'cookie-parser'
There's a lot more to the logs, but I /think/ this is the relevant issue. However, this confuses me, I have cookie-parser in my package.json file.

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 }));

NodeJs App Crashes in module.js Before Executing Procfile Module on Heroku

I've run my NodeJs app locally on my Windows 7 computer without any problem running on 0.8.18. However, when I push it to Heroku and try to visit the URL, the following error is logged:
2013-04-27T19:34:14.073833+00:00 heroku[web.1]: Process exited with status 1
2013-04-27T19:34:59.340168+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=myapp.org fwd="108.212.64.90" dyno= connect= service= status=503 bytes=
2013-04-27T19:35:00.565119+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/favicon.ico host=myapp.org fwd="108.212.64.90" dyno= connect= service= status=503 bytes=
2013-04-27T19:44:23.916316+00:00 heroku[web.1]: Starting process with command `node myApp.js`
2013-04-27T19:44:24.424716+00:00 app[web.1]:
2013-04-27T19:44:24.425324+00:00 app[web.1]: /app/myApp.js:7
2013-04-27T19:44:24.425680+00:00 app[web.1]: ;
2013-04-27T19:44:24.425725+00:00 app[web.1]: ^
2013-04-27T19:44:24.429639+00:00 app[web.1]: SyntaxError: Unexpected token ;
2013-04-27T19:44:24.429639+00:00 app[web.1]: at Module._compile (module.js:437:25)
2013-04-27T19:44:24.429639+00:00 app[web.1]: at Object.Module._extensions..js (module.js:467:10)
2013-04-27T19:44:24.429639+00:00 app[web.1]: at Module.load (module.js:356:32)
2013-04-27T19:44:24.429639+00:00 app[web.1]: at Function.Module._load (module.js:312:12)
2013-04-27T19:44:24.429639+00:00 app[web.1]: at Module.runMain (module.js:492:10)
2013-04-27T19:44:24.429639+00:00 app[web.1]: at process.startup.processNextTick.process._tickCallback (node.js:245:9)
2013-04-27T19:44:25.437926+00:00 heroku[web.1]: Process exited with status 1
2013-04-27T19:44:25.414435+00:00 heroku[web.1]: State changed from starting to crashed
2013-04-27T19:44:22.954367+00:00 heroku[web.1]: State changed from crashed to starting
My procfile simply reads:
web: node myApp.js
My package.json reads:
{
"name": "Arbol",
"version": "0.0.1",
"dependencies": {
"express": "3.1.0",
"bcrypt": "0.7.5",
"connect-flash": "0.1.0",
"sequelize":"1.4.1",
"ejs":"0.8.3",
"oath":"0.2.3",
"node-markdown":"0.1.1",
"pg":"0.15.1",
"passport":"0.1.16",
"passport-twitter":"0.1.4",
"passport-google":"0.3.0",
"passport-facebook":"0.1.5"
},
"engines": {
"node": "0.8.x",
"npm": "1.1.x"
}
}
Additionally, here are the first lines of myApp.js:
console.log('neither here');
var express = require('express'),
app = module.exports = express(),
passport = require('passport'),
flash = require('connect-flash'),
FacebookStrategy = require('passport-facebook').Strategy,
TwitterStrategy = require('passport-twitter').Strategy,
GoogleStrategy = require('passport-google').Strategy;
console.log('nor there');
Neither of the console.log messages are logged when deployed to Heroku.
It seems that nothing in myApp.js is causing this issue, but rather it is being caused by Module. What is Module.js? Where can I find it and why would my app cause a problem in it?
I changed by package.json to explicitly state the exact node version and the problem went away:
"engines": {
"node": "0.8.19",
"npm": "1.1.x"
}

Resources