Skip to main content
  1. About
  2. Stack Internal
The 2026 Annual Developer Survey is live— take the Survey today!

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

MongoDb Connection failed : Error: querySrv ECONNREFUSED

MongoDb Connection failed : Error: querySrv ECONNREFUSED 

How can I solve this error? My node v24.13.0 and mongoose 9.1.5 are the latest versions. Also I changed my network dns to Google's dns number 8.8.8.8, and in my mongodb atlas network acc ip address is 0.0.0.0/0 so that any ip address can access and install Locally in my system.


Here is the connection code:

import express from "express";
import mongoose from "mongoose";
import cors from "cors";
import dotenv from "dotenv";
dotenv.config({ path: "./.env" });
const app = express();
const MONGO = process.env.MONGO_URL;
mongoose
 .connect(MONGO)
 .then(() => {
 console.log("Database Successfully connected");
 })
 .catch((error) => {
 console.error(`MongoDb Connection failed : ${error}`);
 });
app.use(express.json());
app.use(cors());
app.listen(5000, () => {
 console.log(`Server started on port Number : 5000`);
});

error png

Note that when I used this it works perfectly:

import express from "express"; 
import mongoose from "mongoose"; 
import cors from "cors"; 
import dotenv from "dotenv"; 
dotenv.config({ path: "./.env" }); 
 
/* works when I add this */
import dns from "node:dns/promises"; 
dns.setServers(["1.1.1.1", "1.0.0.1"]); 
const app = express(); 
const MONGO = process.env.MONGO_URL; 
mongoose 
 .connect(MONGO) 
 .then(() => { 
 console.log("Database Successfully connected"); 
 }) 
 .catch((error) => { 
 console.error(MongoDb Connection failed : ${error}); 
 }); 
app.use(express.json()); 
app.use(cors()); 
app.listen(5000, () => { 
 console.log(Server started on port Number : 5000); 
});

But the problem is I have so many projects that I already built, so I don't want to add this in every project:

import dns from "node:dns/promises"; 
dns.setServers(["1.1.1.1", "1.0.0.1"]);

Is there another solution?

Answer*

Draft saved
Draft discarded

Required fields are marked with *

Cancel
4
  • 1
    Thanks — the issue was with the Node.js version. After switching to version 20.9.0, everything is working correctly. Commented Feb 17 at 10:18
  • 1
    If you're on Node 20+ with mongodb+srv://, try forcing public DNS before connecting: js import dns from "node:dns"; dns.setServers(["1.1.1.1", "1.0.0.1"]); await mongoose.connect(MONGO_URI); This fixes querySrv ECONNREFUSED caused by SRV lookup failures in newer Node.js DNS resolver. Only affects the current process, not system DNS. Commented Apr 29 at 5:56
  • 1
    Thanks, a lot for this answer, const dns = require("node:dns/promises"); dns.setServers(["1.1.1.1", "1.0.0.1"]); after using this dns perfectly works my server. Commented May 17 at 0:39
  • on my mongoose.ts file, i just pasted added "require("node:dns/promises").setServers(["1.1.1.1", "8.8.8.8"]); " at the top of the file before adding any imports . Commented Jun 8 at 17:43

lang-js

AltStyle によって変換されたページ (->オリジナル) /