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.
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.