I'm trying to deploy my backend express server with mongodb to heroku. it is successful but when I view the webapp I get Application error and the logs in the logs it says this. I have googled around a lot but I cannot seem to find a solution for it
2022-05-27T06:07:55.783858+00:00 app[web.1]: > backend@1.0.0 start
2022-05-27T06:07:55.783858+00:00 app[web.1]: > nodemon server.js
2022-05-27T06:07:55.783859+00:00 app[web.1]:
2022-05-27T06:07:55.790022+00:00 app[web.1]: sh: 1: nodemon: not found
2022-05-27T06:07:55.908150+00:00 heroku[web.1]: Process exited with status 127
2022-05-27T06:07:55.963771+00:00 heroku[web.1]: State changed from starting to crashed
2022-05-27T08:04:57.623823+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/users" host=pure-hamlet-06858.herokuapp.com request_id=190b37a8-410d-4148-bc11-3194e69e9965 fwd="94.254.32.15" dyno= connect= service= status=503 bytes= protocol=https
2022-05-27T08:04:58.440005+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=pure-hamlet-06858.herokuapp.com request_id=d270eb5b-857e-4fd3-95bc-9f1195037626 fwd="94.254.32.15" dyno= connect= service= status=503 bytes= protocol=https
2022-05-27T08:04:59.403877+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/users" host=pure-hamlet-06858.herokuapp.com request_id=7dd2497f-a216-4514-87bd-4823c7ca9d55 fwd="94.254.32.15" dyno= connect= service= status=503 bytes= protocol=https
2022-05-27T08:05:00.192757+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=pure-hamlet-06858.herokuapp.com request_id=4f872a7f-4904-4589-bec3-851c39f404c0 fwd="94.254.32.15" dyno= connect= service= status=503 bytes= protocol=https
I'm guessing it has to do with this error message
heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/users" host=pure-hamlet-06858.herokuapp.com request_id=190b37a8-410d-4148-bc11-3194e69e9965 fwd="94.254.32.15" dyno= connect= service= status=503 bytes= protocol=https
I have a root folder with two subfolders backend and frontend
the root has a json file with these scripts
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node backend/server.js",
"server": "nodemon backend/server.js",
"client": "npm start --prefix frontend",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix frontend && npm run build --prefix frontend"
},
and the backend folder has this a server file and a procfile
// Server side file, import all Routes(Views) and execute them here.
const express = require("express");
const mongoose = require("mongoose");
const dotenv = require("dotenv").config();
const PORT = process.env.PORT || 8081;
const userRouter = require("./routes/user");
const bookRouter = require("./routes/book");
const cors = require("cors");
const app = express();
app.use(express.json());
app.use(cors({ origin: "http://localhost:3000", credentials: true }));
app.use("/user", userRouter);
app.use("/book", bookRouter);
app.use("/uploads", express.static("./uploads"));
app.get("/", (req, res) => {
return res.json({ message: "Hello World " });
});
//Function to start the server
const startServer = (port) => {
try {
app.listen(port, () => {
console.log(`Server up and running at: http://localhost:${port}`);
});
} catch (error) {
console.error(error);
process.exit();
}
};
//server connection starts here
mongoose.connect(process.env.MONGODB_URI).then(() => {
startServer(PORT);
});
The Procfile
web: npm start
and in heroku a have I have the buildpacks
and also the variables
- DATABASE_URL
- PROJECT_PATH