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

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

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!

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!

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.

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