I am working on a deploying a MERN stack application to Heroku. When run locally my project works perfectly but I face the following error when I try to run my application from Heroku.
2020-11-23T01:08:02.199575+00:00 app[web.1]: npm ERR! Failed at the mernshoppinglist@1.0.0 start script.
Here is the full logs from Heroku when I try to load the application
2020-11-23T01:08:02.199331+00:00 app[web.1]: npm ERR!
2020-11-23T01:08:02.199575+00:00 app[web.1]: npm ERR! Failed at the mernshoppinglist@1.0.0 start script.
2020-11-23T01:08:02.199766+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-11-23T01:08:02.875811+00:00 app[web.1]:
2020-11-23T01:08:02.876018+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-11-23T01:08:02.876125+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-11-23T01_08_02_200Z-debug.log
2020-11-23T01:08:02.935584+00:00 heroku[web.1]: Process exited with status 1
2020-11-23T01:08:02.975882+00:00 heroku[web.1]: State changed from starting to crashed
2020-11-23T01:08:03.765808+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=reke-mern-shopping-list.herokuapp.com request_id=0992d8aa-8716-407f-a59e-df58e9e39a54 fwd="208.102.105.218" dyno= connect= service= status=503 bytes= protocol=https
2020-11-23T01:08:04.134057+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=reke-mern-shopping-list.herokuapp.com request_id=ba4d4159-75c3-458c-b3f1-cc1168ac4ca7 fwd="208.102.105.218" dyno= connect= service= status=503 bytes= protocol=https
When I researched this error it was suggested that I add a start script to my package.json. However, I do have that in my package.json. I am curious if anyone would know the solution, OR, just as importantly, what steps I should take in trouble shooting this issue. Heroku deployment has been a real struggle for me. You can see the full file below
{
"name": "mernshoppinglist",
"version": "1.0.0",
"description": "Shopping list with with the MERN stack and JWT",
"main": "server.js",
"scripts": {
"client-install": "npm install --prefix client",
"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": "David REke",
"license": "MIT",
"dependencies": {
"bcryptjs": "^2.4.3",
"concurrently": "^5.3.0",
"config": "^3.3.2",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.10.10"
}
}
I am following along with a tutorial, and the best I can tell I have matched what the author has prescribed. I am not sure if it's needed, but below is also my server.js file.
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const path = require('path');
const config = require('config')
// Bodyparser Middleware
app.use(express.json());
// DB Config
const db = config.get('mongoURI');
// connect to MongoDB
mongoose.connect(db, {useNewUrlParser: true,
useCreateIndex: true})
.then(() => {
console.log('mongoDB Connected...')
})
.catch((err) => {
console.log(err)
});
// use Route
app.use('/api/items', require('./routes/api/items'));
app.use('/api/users', require('./routes/api/users'));
app.use('/api/auth', require('./routes/api/auth'))
// added code from youtube comments
var distDir = __dirname + "/dist/";
app.use(express.static(distDir));
// Server static assets if in production
if(process.env.NODE_ENV === 'production') {
// set static folder
app.use(express.static('client/build'))
app.get('*', (req,res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
} )
}
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`server started on port ${port}`))
Edit: I should add that I don't have a procfile, but my understanding is I do not need one because a start script in my package.json.
Edit 2: When I implemented chmsv's fix I recieved the following errors in my logs:
2020-11-24T13:45:26.235249+00:00 app[worker.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
2020-11-24T13:45:26.235249+00:00 app[worker.1]: at Module.load (internal/modules/cjs/loader.js:879:32)
2020-11-24T13:45:26.235250+00:00 app[worker.1]: at Function.Module._load (internal/modules/cjs/loader.js:724:14)
2020-11-24T13:45:26.235250+00:00 app[worker.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
2020-11-24T13:45:26.235251+00:00 app[worker.1]: at internal/main/run_main_module.js:17:47 {
2020-11-24T13:45:26.235251+00:00 app[worker.1]: code: 'MODULE_NOT_FOUND',
2020-11-24T13:45:26.235251+00:00 app[worker.1]: requireStack: [ '/app/server.js' ]
2020-11-24T13:45:26.235252+00:00 app[worker.1]: }
2020-11-24T13:45:26.307299+00:00 heroku[worker.1]: Process exited with status 1
2020-11-24T13:45:26.357136+00:00 heroku[worker.1]: State changed from up to crashed