MongoDB and Mongoose - Install and Set Up Mongoose

Tell us what’s happening:

I have problem connecting to MongoDB with Replit, it keeps throwing error

Your app is listening on port 3000
node:events:496
throw er; // Unhandled ‘error’ event
^

Error: listen EADDRINUSE: address already in use 0.0.0.0:3000
at Server.setupListenHandle [as _listen2] (node:net:1908:16)
at listenInCluster (node:net:1965:12)
at doListen (node:net:2139:7)
at process.processTicksAndRejections (node:internal/process/task_queues:83:21)
Emitted ‘error’ event on Server instance at:
at emitErrorNT (node:net:1944:8)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
code: ‘EADDRINUSE’,
errno: -98,
syscall: ‘listen’,
address: ‘0.0.0.0’,
port: 3000
}

type or paste code here

here's myapp.js :

const express = require("express");
const mongoose = require("mongoose");

const app = express();
const PORT = process.env.PORT || 3000; 

// MongoDB connection string
const mongoDBUrl =
  "mongodb+srv://myusername:mypass@cluster0.z93x8.mongodb.net/myDatabase?retryWrites=true&w=majority";

// Connect to MongoDB
mongoose
  .connect(mongoDBUrl, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => console.log("Connected to MongoDB Atlas"))
  .catch((err) => console.error("Could not connect to MongoDB Atlas:", err));

// Listen on the designated port
app.listen(PORT, "0.0.0.0", () => {
  console.log(`Your app is listening on port ${PORT}`);
});

here's my .env :

MONGO_URI='mongodb+srv://myusername:mypass@cluster0.z93x8.mongodb.net/?retryWrites=true&w=majority'


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36

Challenge Information:

MongoDB and Mongoose - Install and Set Up Mongoose

Don’t remember this much but some other process might be using port 3000. but maybe if you change the PORT to 3001 this issue will clear. Don’t know if this will work but that’s troubleshooting.

Also:

MONGO_URI='mongodb+srv://myusername:mypass@cluster0.z93x8.mongodb.net/?retryWrites=true&w=majority'

Is this the actual MONGO_URI you are using?

i changed to be 3001 but now i got this error:

Your app is listening on port 3000
Your app is listening on port 3001
(node:1528) [DEP0170] DeprecationWarning: The URL mongodb://myuser:mypass@cluster0-shard-00-01.z93x8.mongodb.net:27017,cluster0-shard-00-02.z93x8.mongodb.net:27017,cluster0-shard-00-00.z93x8.mongodb.net:27017/fcc-mongo-and-mongoose?authSource=admin&replicaSet=atlas-uua04u-shard-0&retryWrites=true&w=majority&ssl=true is invalid. Future versions of Node.js will throw an error.
(Use node --trace-deprecation ... to show where the warning was created)
Connected to MongoDB Atlas
^

Also: no, i redacted my real pass , username and databse name

I think the node:1528) [DEP0170] DeprecationWarning: The URL... should not be an issue. Have you tried submitting the challege ?

Did you pass the challege? If not there is an alternative of deleting the workspace. I just went over that challege and deleting the workspace and starting a new one helped me pass the challege.

hey, so i changed the node version from mongodb to earlier version 2.2.12 and modified the MONGO_URI

MONGO_URI='mongodb://myusername:password@cluster0-shard-00-00.mongodb.net:27017,cluster0-shard-00-01.mongodb.net:27017,cluster0-shard-00-02.mongodb.net:27017/mydatabase?ssl=true&replicaSet=atlas-XXXXX-shard-0&authSource=admin&retryWrites=true&w=majority'

It’s all passed now :grinning: thanks!

1 Like

Just to be clear, you should not start the server in myApp.js. It is already started for you in server.js.

1 Like