I am following this MERN tutorial: https://www.youtube.com/watch?v=ngc9gnGgUdA
Around @18:00 is where I am having problems. After some research I think some of the code in the video is outdated and I am doing my best to get this to work! This is my current error: Error: listen EADDRINUSE: address already in use :::5000
import express from 'express';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import cors from 'cors';
import postRoutes from './routes/posts.js';
const app = express();
app.use('/posts', postRoutes);
app.use(bodyParser.json( { limit: "30mb", extended: true}));
app.use(bodyParser.urlencoded( { limit: "30mb", extended: true}));
app.use(cors());
const CONNECTION_URL = 'mongodb+srv://test:[email protected]/myFirstDatabase?retryWrites=true&w=majority';
const PORT = process.env.PORT || 5000;
mongoose.createConnection(CONNECTION_URL).asPromise();
mongoose.connect(CONNECTION_URL)
.then(() => app.listen(PORT, () => console.log(`server running on port: ${PORT}` )))
.catch((error) => console.log(error.message));
I SUPER appreciate any and all help. If anyone can point me in the direction of a better tutorial that would be amazing! I am a novice code, I finished a virtual coding bootcamp recently, but I need to make some real apps. Thanks again!
2
Answers
First I would check and see if you already have something running on port 5000, to confirm, I would run
npx kill-port 5000
, then try to run port 5000 again to see if that port is now free after killing it via the previously noted command…BEFORE:
Run
npm start
in the terminal and check the output to see if the terminal print something likeserver.js nodemon src/server.js
.REASON:
If you do then your scripts are calling
server.js
twice, which is causing this Error.FIX:
I had the same issue and fixed it by fixing my
package.json
in root folder and also thepackage.json
in the server folder.AFTER:
you should see the terminal print
nodemon src/server.js
when you run your script.Hope this helps.