ExpressJS / Heroku Application Error - node.js

I am attempting to deploy a test express app on Heroku.
My package.json looks like this -
{
"name": "heroku-test-app",
"version": "1.0.0",
"description": "",
"main": "dist",
"scripts": {
"test": "cross-env NODE_ENV=test nodemon --exec mocha ./**/*.spec.js --compilers js:babel-core/register --require babel-polyfill",
"dev": "nodemon -w src --exec \"babel-node src --presets es2015,stage-0\"",
"build": "babel src -s -D -d dist --presets es2015,stage-0",
"start": "cross-env node dist",
"prestart": "npm run -s build",
"clean": "rimraf dist/"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^7.2.3",
"babel-polyfill": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"body-parser": "^1.17.2",
"cross-env": "^5.0.5",
"dotenv": "^4.0.0",
"express": "^4.15.4",
"morgan": "^1.8.2"
},
"devDependencies": {
"chai": "^4.1.1",
"mocha": "^3.5.0",
"nodemon": "^1.11.0",
"rimraf": "^2.6.1",
"supertest": "^3.0.0"
}
}
My Procfile looks like this -
web: npm start
and my test app is as so -
import express from 'express';
import morgan from 'morgan';
import bodyParser from 'body-parser';
const app = express();
const postBodyLimit = '100kb';
const port = 3000;
app.use(
morgan('combined', {
skip: (req, res) => res.statusCode < 400
})
);
app.use(bodyParser.json({ limit: postBodyLimit }));
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/', (req, res) => res.status(200).json({ message: 'No unicorns here.' }));
// catch 404's and pass to our global error handler
app.all('*', (req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use((err, req, res, next) => {
res.status(err.status || 500).json({
message: err.message,
error: app.get('env') === 'development' ? err : null
});
});
app.listen(port, () => console.log(`listening on port ${port}`));
export default app;
When running my app locally, it starts and works perfectly. When running heroku local web, it also works as expected.
When I deploy to heroku however, I get the application error page. However on streaming my logs, there is nothing to suggest it has crashed.
I have just deployed again now, this is the current log file
2017-08-28T06:38:40.000000+00:00 app[api]: Build succeeded
2017-08-28T06:39:03.959631+00:00 app[web.1]:
2017-08-28T06:39:03.959649+00:00 app[web.1]: > heroic-test-app#1.0.0 prestart /app
2017-08-28T06:39:03.959650+00:00 app[web.1]: > npm run -s build
2017-08-28T06:39:03.959651+00:00 app[web.1]:
2017-08-28T06:39:05.856118+00:00 app[web.1]: src/index.js -> dist/index.js
2017-08-28T06:39:05.898228+00:00 app[web.1]:
2017-08-28T06:39:05.898232+00:00 app[web.1]: > heroic-test-app#1.0.0 start /app
2017-08-28T06:39:05.898234+00:00 app[web.1]: > cross-env node dist
2017-08-28T06:39:05.898234+00:00 app[web.1]:
2017-08-28T06:39:06.654100+00:00 app[web.1]: listening on port 3000
2017-08-28T06:44:40.078283+00:00 app[web.1]:
2017-08-28T06:44:40.078319+00:00 app[web.1]: > heroic-test-app#1.0.0 prestart /app
2017-08-28T06:44:40.078320+00:00 app[web.1]: > npm run -s build
2017-08-28T06:44:40.078321+00:00 app[web.1]:
2017-08-28T06:44:42.379605+00:00 app[web.1]: src/index.js -> dist/index.js
2017-08-28T06:44:42.434704+00:00 app[web.1]:
2017-08-28T06:44:42.434707+00:00 app[web.1]: > heroic-test-app#1.0.0 start /app
2017-08-28T06:44:42.434708+00:00 app[web.1]: > cross-env node dist
2017-08-28T06:44:42.434709+00:00 app[web.1]:
2017-08-28T06:44:43.156940+00:00 app[web.1]: listening on port 3000
2017-08-28T06:45:39.332668+00:00 app[web.1]: Error waiting for process to terminate: No child processes
I am at a loss how to proceed at this point.

you shouldn't set the port of http server as 3000, you should use the port of process.env, heroku will set an PORT in process.env.
So you should use app.listen(process.env.PORT, () => {}).
and there is an err code about the heroku application, if you can provide that, it would be better.

Related

Error: listen EACCES: permission denied 5000; how to solve this nodejs error?

I'm trying to run my node server run, but suddenly i have got this error. How can i solve this error? when i command npm run start-dev show me this error. i have change this port, but got same error. Why happend this error? Please help me out, how can i solve this?
here is the error,
node:events:491
throw er; // Unhandled 'error' event
^
Error: listen EACCES: permission denied 30000;
at Server.setupListenHandle [as _listen2] (node:net:1468:21)
at listenInCluster (node:net:1533:12)
at Server.listen (node:net:1632:5)
at Function.listen (D:\ecommerce-api\node_modules\express\lib\application.js:635:24)
at StartDB (D:\ecommerce-api\app.js:34:9)
at Object.<anonymous> (D:\ecommerce-api\app.js:42:1)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1512:8)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
code: 'EACCES',
errno: -4092,
syscall: 'listen',
address: '5000;',
port: -1
}
Node.js v18.12.1
[nodemon] app crashed - waiting for file changes before starting...
my env file
PORT=5000;
MONGO_URI=mongodb://127.0.0.1:27017/ecommerce
App js file
require("dotenv").config();
const express = require("express");
const app = express();
const port = process.env.PORT || 5000;
//Database
const connectDB = require("./DB/ConnectDB");
//Paths
const authRoute = require("./Routes/AuthRoute");
//Middleware
app.use(express.json());
app.get("/", (req, res) => {
res.send("Ecommerce API!");
});
app.use("/api/v1/auth", authRoute);
const StartDB = async () => {
try {
connectDB(process.env.MONGO_URI);
app.listen(port, () => {
console.log(`App listening on port ${port}`);
});
} catch (error) {
console.log(error);
}
};
StartDB();
the package json file below.
my pacakge json file
{
"name": "ecommerce-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js",
"start-dev": "nodemon app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express-async-errors": "^3.1.1",
"http-status-codes": "^2.2.0",
"jsonwebtoken": "^9.0.0",
"mongoose": "^6.8.4",
"morgan": "^1.10.0",
"validator": "^13.7.0"
}
}
I got the same error , do not add any spaces in (.env) file , then you should get the answer

Deploying to Heroku: MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"

I am having issues deploying to Heroku. I have uploaded the relevant env variables under "Config Vars" within Heroku Dashboard but am wondering why I am still getting hit with the following error below:
2022-07-22T06:47:59.068313+00:00 heroku[web.1]: Starting process with command `npm start`
2022-07-22T06:48:00.732711+00:00 app[web.1]:
2022-07-22T06:48:00.732804+00:00 app[web.1]: > project-management-webapp#1.0.0 start
2022-07-22T06:48:00.732804+00:00 app[web.1]: > npm install && cd server && node index.js
2022-07-22T06:48:00.732805+00:00 app[web.1]:
2022-07-22T06:48:01.693914+00:00 app[web.1]:
2022-07-22T06:48:01.693932+00:00 app[web.1]: up to date, audited 128 packages in 601ms
2022-07-22T06:48:01.694072+00:00 app[web.1]:
2022-07-22T06:48:01.694106+00:00 app[web.1]: 14 packages are looking for funding
2022-07-22T06:48:01.694126+00:00 app[web.1]: run `npm fund` for details
2022-07-22T06:48:01.695406+00:00 app[web.1]:
2022-07-22T06:48:01.695406+00:00 app[web.1]: found 0 vulnerabilities
2022-07-22T06:48:02.296149+00:00 heroku[web.1]: State changed from starting to crashed
2022-07-22T06:48:02.083051+00:00 app[web.1]: Server running on port 24830
2022-07-22T06:48:02.087994+00:00 app[web.1]: /app/node_modules/mongodb-connection-string-url/lib/index.js:86
2022-07-22T06:48:02.088006+00:00 app[web.1]: throw new MongoParseError('Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"');
2022-07-22T06:48:02.088007+00:00 app[web.1]: ^
2022-07-22T06:48:02.088007+00:00 app[web.1]:
2022-07-22T06:48:02.088013+00:00 app[web.1]: MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"
2022-07-22T06:48:02.088013+00:00 app[web.1]: at new ConnectionString (/app/node_modules/mongodb-connection-string-url/lib/index.js:86:19)
2022-07-22T06:48:02.088014+00:00 app[web.1]: at parseOptions (/app/node_modules/mongodb/lib/connection_string.js:209:17)
2022-07-22T06:48:02.088014+00:00 app[web.1]: at new MongoClient (/app/node_modules/mongodb/lib/mongo_client.js:62:63)
2022-07-22T06:48:02.088015+00:00 app[web.1]: at /app/node_modules/mongoose/lib/connection.js:796:16
2022-07-22T06:48:02.088015+00:00 app[web.1]: at new Promise (<anonymous>)
2022-07-22T06:48:02.088016+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:793:19)
2022-07-22T06:48:02.088016+00:00 app[web.1]: at /app/node_modules/mongoose/lib/index.js:379:10
2022-07-22T06:48:02.088016+00:00 app[web.1]: at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
2022-07-22T06:48:02.088016+00:00 app[web.1]: at new Promise (<anonymous>)
2022-07-22T06:48:02.088021+00:00 app[web.1]: at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
2022-07-22T06:48:02.228355+00:00 heroku[web.1]: Process exited with status 1
Below is the relevant code:
INDEX.JS
const express = require('express')
const colors = require('colors')
const { graphqlHTTP } = require('express-graphql')
const cors = require('cors')
const schema = require('./schema/schema')
const connectDB = require('./config/db')
require('dotenv').config()
const app = express()
// MongoDB
connectDB()
// CORS
app.use(cors())
// GraphQL
app.use(
'/graphql',
graphqlHTTP({
schema: schema,
graphiql: process.env.NODE_ENV === 'development',
})
)
app.listen(
process.env.PORT || 4000,
console.log(`Server running on port ${process.env.PORT}`)
)
DB.JS
const mongoose = require('mongoose')
const connectDB = async () => {
const conn = await mongoose.connect(process.env.MONGODB_URI)
console.log(`MongoDB Connected: ${conn.connection.host}`.cyan.underline.bold)
}
module.exports = connectDB
PACKAGE.JSON
{
"name": "project-management-webapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "npm install && cd server && node index.js",
"dev": "nodemon server/index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/BIGWALDOR/project-management-webapp.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/BIGWALDOR/project-management-webapp/issues"
},
"homepage": "https://github.com/BIGWALDOR/project-management-webapp#readme",
"dependencies": {
"colors": "^1.4.0",
"cors": "^2.8.5",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"express-graphql": "^0.12.0",
"graphql": "^15.8.0",
"mongoose": "^6.4.5"
},
"devDependencies": {
"nodemon": "^2.0.19"
}
}
I'm definitely at a loss here so any help would be much appreciated!

Deploy node.js express backend to heroku

Its my first time deploying on heroku and i keep getting errors, the build is succeeding but when the build finishes and i try to view the API response in the browser via heroku i get back an error of - Failed to load resource: the server responded with a status of 500 (Internal Server Error)
logs from heroku -
2021-12-11T19:16:54.702260+00:00 app[web.1]: npm ERR! errno 1
2021-12-11T19:16:54.708322+00:00 app[web.1]: npm ERR! example-create-react-app-express#1.0.0 start: node server.js
2021-12-11T19:16:54.708419+00:00 app[web.1]: npm ERR! Exit status 1
2021-12-11T19:16:54.708524+00:00 app[web.1]: npm ERR!
2021-12-11T19:16:54.708602+00:00 app[web.1]: npm ERR! Failed at the example-create-react-app-express#1.0.0 start script.
2021-12-11T19:16:54.708721+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-12-11T19:16:54.718471+00:00 app[web.1]:
2021-12-11T19:16:54.719294+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-12-11T19:16:54.719298+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-12-11T19_16_54_709Z-debug.log
2021-12-11T19:16:54.908600+00:00 heroku[web.1]: Process exited with status 1
2021-12-11T19:16:55.067169+00:00 heroku[web.1]: State changed from starting to crashed
2021-12-11T19:16:56.373561+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/api/hello" host=agile-spire-07157.herokuapp.com request_id=ab0c71fb-1be1-4829-b123-e57cc4e7fafd fwd="67.177.196.118" dyno= connect= service= status=503 bytes= protocol=https
2021-12-11T19:17:56.820732+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=agile-spire-07157.herokuapp.com request_id=b9615ee7-190d-44ce-8d97-c099d26149ca fwd="67.177.196.118" dyno= connect= service= status=503 bytes= protocol=https
Any help at all is greatly appreciated
Folder structure:
server.js:
const express = require('express');
const axios = require('axios');
require('dotenv').config();
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
app.use(cors());
app.use(express.static(path.join(__dirname, 'client/build')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'));
});
const port = process.env.PORT || 2000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const apiKey = process.env.APIKEY
app.get('/api/hello', (req, res) => {
const { term, location } = req.query
const config = {
method: 'get',
url: `https://api.yelp.com/v3/businesses/search?term=${term}&location=${location}`,
headers: {
'Authorization': `Bearer ${apiKey}`
}
};
axios(config)
.then(function (response) {
return JSON.stringify(response.data, null, 2)
})
.then(function (jsonResponse) {
res.send(jsonResponse)
})
.catch(function (error) {
console.log(error);
});
});
app.listen(port, () => console.log(`Listening on port ${port}`))
package.json for server:
{
"name": "example-create-react-app-express",
"version": "1.0.0",
"scripts": {
"start": "node server.js",
"client": "cd ../client && yarn start",
"server": "nodemon server.js",
"dev": "concurrently --kill-others-on-fail \"yarn server\" \"yarn client\""
},
"engines": {
"node": "14.17.3"
},
"dependencies": {
"axios": "^0.24.0",
"body-parser": "^1.18.3",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.1"
},
"devDependencies": {
"concurrently": "^4.0.1"
}
}
So i think i figured it out, it seems to be working and heres what i changed:
I added a Procfile in the root directory (make sure its a capital P) and added this to it -> web: node server.js
(so just put web: node(whatever your server file is named)
If you are storing your API key in a .env file then you have to let heroku know youre doing just that. You can let heroku know this by typing this command in terminal while in server folder
heroku config:set <key=value> If you did this right youll get back a response from heroku saying just that. This is a link i followed instructions with that worked for me https://dzone.com/articles/deploy-your-node-express-app-on-heroku-in-8-easy-s
Final server.js
const express = require('express');
const axios = require('axios');
require('dotenv').config();
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
app.use(cors());
const port = process.env.PORT || 2000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const apiKey = process.env.APIKEY
app.get('/api/hello', (req, res) => {
const { term, location } = req.query
const config = {
method: 'get',
url: `https://api.yelp.com/v3/businesses/search?term=${term}&location=${location}`,
headers: {
'Authorization': `Bearer ${apiKey}`
}
};
axios(config)
.then(function (response) {
return JSON.stringify(response.data, null, 2)
})
.then(function (jsonResponse) {
res.send(jsonResponse)
})
.catch(function (error) {
console.log(error);
});
});
app.listen(process.env.PORT || 2000, () => console.log(`Listening on port ${port}`))
Package.json for server:
{
"name": "example-create-react-app-express",
"version": "1.0.0",
"engines": {
"node": "14.17.3"
},
"scripts": {
"start": "node server.js",
"client": "cd ../client && yarn start",
"server": "nodemon server.js",
"dev": "concurrently --kill-others-on-fail \"yarn server\" \"yarn client\""
},
"dependencies": {
"axios": "^0.24.0",
"body-parser": "^1.18.3",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.1"
},
"devDependencies": {
"concurrently": "^4.0.1"
}
}

How to connect heroku server to mongodb?

Beginner with node.js I have an issue to deploy server on heroku.
I made a server.js application which use mongodb.
It's work fine when I run the server on local (http://localhost:3000) with "npm start" cmd, I'm able to connect to my mongo database by the command :
mongoose.connect(
'mongodb+srv://yolanpibrac:mypassword#cluster0-7z1th.mongodb.net/test? retryWrites=true&w=majority';,
{ useNewUrlParser: true })
(I followed this tutorial : [https://appdividend.com/2018/04/14/how-to-deploy-nodejs-app-to-heroku/])
But when i deploy my app on heroku, the app can not connect to mongo db. I have the following error (by checking : heroku logs -t) :
2019-08-06T21:06:33.086146+00:00 heroku[web.1]: Starting process with command `node src/server.js`
2019-08-06T21:06:35.404641+00:00 heroku[web.1]: State changed from starting to up
2019-08-06T21:06:35.058357+00:00 app[web.1]: Listening on port 26845
2019-08-06T21:06:36.144056+00:00 app[web.1]: Error while DB connecting
2019-08-06T21:06:36.149809+00:00 app[web.1]: { MongoNetworkError: failed to connect to server [cluster0-shard-00-00-7z1th.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to cluster0-shard-00-00- 7z1th.mongodb.net:27017 closed]
2019-08-06T21:06:36.149812+00:00 app[web.1]: at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:431:11)
2019-08-06T21:06:36.149814+00:00 app[web.1]: at Pool.emit (events.js:189:13)
2019-08-06T21:06:36.149815+00:00 app[web.1]: at connect (/app/node_modules/mongodb-core/lib/connection/pool.js:557:14)
2019-08-06T21:06:36.149816+00:00 app[web.1]: at callback (/app/node_modules/mongodb-core/lib/connection/connect.js:109:5)
2019-08-06T21:06:36.149818+00:00 app[web.1]: at runCommand (/app/node_modules/mongodb-core/lib/connection/connect.js:129:7)
2019-08-06T21:06:36.149819+00:00 app[web.1]: at Connection.errorHandler (/app/node_modules/mongodb-core/lib/connection/connect.js:321:5)
2019-08-06T21:06:36.149820+00:00 app[web.1]: at Object.onceWrapper (events.js:277:13)
2019-08-06T21:06:36.149821+00:00 app[web.1]: at Connection.emit (events.js:189:13)
2019-08-06T21:06:36.149823+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:350:12)
2019-08-06T21:06:36.149824+00:00 app[web.1]: at Object.onceWrapper (events.js:277:13)
2019-08-06T21:06:36.149825+00:00 app[web.1]: at TLSSocket.emit (events.js:189:13)
2019-08-06T21:06:36.149826+00:00 app[web.1]: at _handle.close (net.js:613:12)
2019-08-06T21:06:36.149828+00:00 app[web.1]: at TCP.done (_tls_wrap.js:386:7)
2019-08-06T21:06:36.149829+00:00 app[web.1]: name: 'MongoNetworkError',
2019-08-06T21:06:36.149830+00:00 app[web.1]: errorLabels: [ 'TransientTransactionError' ],
2019-08-06T21:06:36.149832+00:00 app[web.1]: [Symbol(mongoErrorContextSymbol)]: {} }
I already read all issues related to this problem, but none of the solution worked.
I have mongodb account with cluster connexion string
I was carefull in my password that any special characters are URL encoded.
I have a procfile : web: node src/server.js and an index.html file
my server.js file :
//Définition des modules
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require('body-parser');
const app = express();
const path = require('path');
const dbRoute = 'mongodb+srv://yolanpibrac:mypassword#cluster0- 7z1th.mongodb.net/test?retryWrites=true&w=majority';
const dbRouteLocal = 'mongodb://localhost/db';
//Connexion à la base de donnée
mongoose.connect(dbRoute, { useNewUrlParser: true }).then(() => {
console.log('Connected to mongoDB')
}).catch(e => {
console.log('Error while DB connecting');
console.log(e);
});
var urlencodedParser = bodyParser.urlencoded({
extended: true
});
app.use(urlencodedParser);
app.use(bodyParser.json({limit: '10mb', extended: true}));
//Définition des CORS
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Headers', 'Origin,X-Requested- With,content-type,Accept,content-type,application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
var router = express.Router();
app.use('/user', router);
require(__dirname + '/controllers/userController')(router);
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});
//Définition et mise en place du port d'écoute
var port = process.env.PORT || "3000";
app.listen(port, () => console.log(`Listening on port ${port}`));
my package.json :
{
"name": "movies-displayer",
"version": "0.1.0",
"engines": {
"node": "11.4.0"
},
"private": true,
"proxy": "http://yolan-pibrac.com",
"dependencies": {
"animated": "^0.2.2",
"axios": "^0.19.0",
"body-parser": "^1.19.0",
"bootstrap": "^4.3.1",
"express": "^4.17.1",
"jwt-simple": "^0.5.6",
"mongodb": "^3.3.0-beta2",
"mongoose": "^5.6.8",
"password-hash": "^1.2.2",
"react": "^16.8.6",
"react-activity": "^1.2.2",
"react-animations": "^1.0.0",
"react-bootstrap": "^0.32.1",
"react-bootstrap-dropdown-menu": "^1.1.15",
"react-dom": "^16.8.6",
"react-multi-carousel": "^2.1.2",
"react-popper": "^1.3.3",
"react-pose": "^4.0.8",
"react-redux": "^7.0.3",
"react-router": "^4.3.1",
"react-router-dom": "^4.3.1",
"react-scripts": "3.0.1",
"react-state-animation": "^0.1.0",
"reactstrap": "^8.0.1",
"redux": "^4.0.1",
"styled-components": "^4.3.2"
},
"scripts": {
"start": "node src/server.js"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
I can provide more details if needed !
Thank you for your help,
You may need to allow heroku to access your mongodb server.
You can do this by logging in to Mongodb atlas and clicking on Network access on the left side under security settings, then click add ip address on the right top and enter 0.0.0.0/0 as the ip address to allow access from all ip's a or add your heroku servers' specific ip if you can get it.

My node + express app deploys fine locally but will not deploy when pushed to heroku

first off my apologies if this is a basic solve. I'm new to Node + Express and still trying to grapple a few concepts!
I have a Node + Express app that starts locally but when I push to heroku or run heroku local web it exits with code 0 and produces no error code for me to diagnose.
Here is the specific output from the logs when I deploy to Heroku:
2018-06-30T11:58:37.052754+00:00 heroku[web.1]: Starting process with command `npm run build`
2018-06-30T11:58:39.336703+00:00 app[web.1]:
2018-06-30T11:58:39.336728+00:00 app[web.1]: > bookworm-api#1.0.0 build /app
2018-06-30T11:58:39.336733+00:00 app[web.1]: > npm run clean && npm run build-babel
2018-06-30T11:58:39.336734+00:00 app[web.1]:
2018-06-30T11:58:39.777160+00:00 app[web.1]:
2018-06-30T11:58:39.777199+00:00 app[web.1]: > bookworm-api#1.0.0 clean /app
2018-06-30T11:58:39.777201+00:00 app[web.1]: > rm -rf build && mkdir build
2018-06-30T11:58:39.777202+00:00 app[web.1]:
2018-06-30T11:58:40.305583+00:00 app[web.1]:
2018-06-30T11:58:40.305603+00:00 app[web.1]: > bookworm-api#1.0.0 build-babel /app
2018-06-30T11:58:40.305605+00:00 app[web.1]: > babel -d ./build ./src -s
2018-06-30T11:58:40.305610+00:00 app[web.1]:
2018-06-30T11:58:41.146122+00:00 app[web.1]: src/index.js -> build/index.js
2018-06-30T11:58:41.196474+00:00 app[web.1]: src/mailer.js -> build/mailer.js
2018-06-30T11:58:41.242714+00:00 app[web.1]: src/models/Circuit.js -> build/models/Circuit.js
2018-06-30T11:58:41.293304+00:00 app[web.1]: src/models/User.js -> build/models/User.js
2018-06-30T11:58:41.346985+00:00 app[web.1]: src/routes/auth.js -> build/routes/auth.js
2018-06-30T11:58:41.362022+00:00 app[web.1]: src/routes/circuits.js -> build/routes/circuits.js
2018-06-30T11:58:41.380720+00:00 app[web.1]: src/routes/stripe.js -> build/routes/stripe.js
2018-06-30T11:58:41.411474+00:00 app[web.1]: src/routes/users.js -> build/routes/users.js
2018-06-30T11:58:41.423692+00:00 app[web.1]: src/utils/parseErrors.js -> build/utils/parseErrors.js
2018-06-30T11:58:41.508066+00:00 heroku[web.1]: Process exited with status 0
2018-06-30T11:58:41.532442+00:00 heroku[web.1]: State changed from starting to crashed
Here is the output when I run Heroku local web
[OKAY] Loaded ENV .env File as KEY=VALUE Format
8:04:38 AM web.1 | > bookworm-api#1.0.0 build /Users/kylependergast/Desktop/projects/bookworm/bookworm-api
8:04:38 AM web.1 | > npm run clean && npm run build-babel
8:04:38 AM web.1 | > bookworm-api#1.0.0 clean /Users/kylependergast/Desktop/projects/bookworm/bookworm-api
8:04:38 AM web.1 | > rm -rf build && mkdir build
8:04:39 AM web.1 | > bookworm-api#1.0.0 build-babel /Users/kylependergast/Desktop/projects/bookworm/bookworm-api
8:04:39 AM web.1 | > babel -d ./build ./src -s
8:04:39 AM web.1 | src/index.js -> build/index.js
8:04:39 AM web.1 | src/mailer.js -> build/mailer.js
8:04:39 AM web.1 | src/models/Circuit.js -> build/models/Circuit.js
8:04:39 AM web.1 | src/models/User.js -> build/models/User.js
8:04:39 AM web.1 | src/routes/auth.js -> build/routes/auth.js
8:04:39 AM web.1 | src/routes/circuits.js -> build/routes/circuits.js
8:04:39 AM web.1 | src/routes/stripe.js -> build/routes/stripe.js
8:04:39 AM web.1 | src/routes/users.js -> build/routes/users.js
8:04:39 AM web.1 | src/utils/parseErrors.js -> build/utils/parseErrors.js
8:04:39 AM web.1 Exited Successfully
Here is my package.json file:
{
"name": "bookworm-api",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"test": "nodemon --exec babel-node -- src/index.js",
"clean": "rm -rf build && mkdir build",
"build-babel": "babel -d ./build ./src -s",
"build": "npm run clean && npm run build-babel",
"start": "npm run build && node ./build/index.js"
},
"engines": {
"node": "9.4.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-eslint": "^8.2.3",
"babel-preset-env": "^1.7.0",
"eslint-plugin-jsx-a11y": "^5.1.1",
"eslint-plugin-prettier": "^2.6.0",
"nodemon": "^1.17.4"
},
"dependencies": {
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"bcrypt": "^2.0.1",
"bluebird": "^3.5.1",
"body-parser": "^1.18.2",
"dotenv": "^5.0.1",
"express": "^4.16.3",
"jsonwebtoken": "^8.2.1",
"lodash": "^4.17.10",
"mongodb": "^3.1.0",
"mongoose": "^4.11.10",
"mongoose-unique-validator": "^2.0.1",
"nodemailer": "^4.6.4",
"request-promise": "^4.2.2",
"stripe": "^6.1.1",
"uuid": "^3.2.1",
"xml2js": "^0.4.19"
}
}
And here is my index.js file:
import express from 'express';
import path from 'path';
import mongoose from 'mongoose';
import bodyParser from 'body-parser';
import dotenv from 'dotenv';
import Promise from 'bluebird';
import auth from './routes/auth';
import users from './routes/users';
import circuits from "./routes/circuits";
import stripe from './routes/stripe';
dotenv.config();
const app = express();
app.use(bodyParser.json());
mongoose.Promise = Promise;
mongoose.connect(
process.env.MONGODB_URI || process.env.MONGODB_URL_DEV_LOCAL,
{useMongoClient: true});
app.use('/api/auth', auth);
app.use('/api/users', users);
app.use('/api/circuits', circuits);
app.use('/api/stripe', stripe);
app.post('/api/auth', (req, res) => {
res.status(400).json({ errors: {global: "Invalid Credentials" } });
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
})
var port = process.env.PORT || 8080
app.listen(port || 8080, function(){
console.log("App is running on port " + port);
});
Here is my Procfile
web: npm run build
I'm sure someone else has run into this issue but I've been unable to find resources that suggest what next steps to take. If you can point me to a relevant resource or provide the answer yourself it would be very much appreciated!
Your Procfile seems to be defined such that you're building stuff when starting the process. The build succeeds (returns 0), but then the process stops, which Heroku thinks is a bad condition, cause web servers shouldn't stop.
You need to actually run the web server in Heroku. Something like web: npm run start.
Though I would separate the build & run stages. You should do the build locally, or on a build machine (or a build service, such as Travis, Circle etc - they have Heroku integrations as well), and then push something compiled to Heroku. So Procfile could only be web: node ./build/index.js or something like that.

Resources