I am setting up a server and every time I try running npm start
I get the following error:
[nodemon] 2.0.12
[nodemon] to restart at any time, enter
`rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
[nodemon] clean exit - waiting for changes before restart
I have researched online such as StackOverflow, YouTube and even Reddit. However I do not understand why I am getting this error, since I added app.listen()
. I have also ran npm update
to make sure that I have the most up to date npm version.
In addition, it seems that I need to revert to an older version of Nodemon, but I am wondering if there is a better way of doing this with more recent versions?
Here is what my code looks like:
index.js:
import express from "express";
import bodyParser from "body-parser";
import mongoose from "mongoose";
import cors from "cors";
const app = express();
app.use(express.json({ limit: "30mb", extended: true }));
app.use(express.urlencoded({ limit: "30mb", extended: true }));
app.use(cors());
// database connection string: PLEASE NOTE THAT I HAVE REPLACED MY PASSWORD WITH THE PROPER ONE HERE
const CONNECTION_URL =
"mongodb srv://admin2021:<password>@cluster0.haf0y.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
const PORT = process.env.PORT || 5000;
// connect to database
mongoose
.connect(CONNECTION_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() =>
app.listen(PORT, () => console.log(`Server running on port: ${PORT}`))
)
.catch(() => (error) => console.log(error.message));
package.json:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "nodemon ./index.js"
},
// etc.
}
Please note that I tried replacing nodemon ./index.js
with nodemon index.js
but I obtain the same error.
Any help is appreciated.
CodePudding user response:
// connect to database
try {
await mongoose.connect(url, options);
} catch (error) {
console.log('connnection error', error)
}
app.listen(PORT, () => console.log(`Server running on port: ${PORT}`))
Try this