Mongoose querySrv ECONNREFUSED with Node v22.22.0 on Windows - Works on Render but fails locally
I'm facing a persistent connection issue with Mongoose and MongoDB Atlas ever since I updated/reinstalled Node.js to version v22.22.0. Interestingly, the exact same code works perfectly when deployed on Render, but fails on my local machine (Windows).
The Error:
Connexion échouée Error: querySrv ECONNREFUSED _mongodb._tcp.cluster0.nkgia.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (node:internal/dns/promises:294:17) {
code: 'ECONNREFUSED',
syscall: 'querySrv',
hostname: '_mongodb._tcp.cluster0.nkgia.mongodb.net'
}
What I have tried so far:
IP Whitelisting: My current IP is added in Atlas, and I even tried 0.0.0.0/0 (Allow all).
Network: I switched from my home Wi-Fi to a mobile 4G hotspot to rule out router firewall issues.
DNS Fixes: * Added dns.setDefaultResultOrder('ipv4first'); at the top of my entry file.
Used family: 4 in mongoose connection options.
Ran the app with NODE_OPTIONS="--dns-result-order=ipv4first".
Environment Cleanup: Deleted node_modules and package-lock.json and performed a fresh npm install.
Connection String: Tried both mongodb+srv:// and the long standard connection string. The long string results in a MongooseServerSelectionError: Server selection timed out.
Environment:
Node.js: v22.22.0
Mongoose: 8.x (latest)
OS: Windows 10/11
Database: MongoDB Atlas (Shared Cluster)
It seems like a DNS resolution issue specific to Node v22's internal handling on Windows. Has anyone found a workaround for this version, or is there a specific Windows network setting I should check?
Thanks in advance!