Error: querySrv ENOTFOUND _mongodb._tcp.*******

When I try to run any program of Node.js in my machine it give this error, I tried using nodemon to see if it works using nodemon but it does not work. I have check and the code is working my other machines but not this one for some reason.

Error:

node:internal/dns/promises:293
this.reject(new DNSException(err, this.bindingName, this.hostname));
^

Error: querySrv ENOTFOUND _mongodb._tcp.********

at QueryReqWrap.onresolve [as oncomplete] (node:internal/dns/promises:293:17) {

errno: undefined,
code: ‘ENOTFOUND’,
syscall: ‘querySrv’,
hostname: ‘************’
}

Node.js v22.5.1

The issue is due to the DNS resolution for the mongodb+srv:// URI scheme failing. See querySrv errors when connecting to MongoDB Atlas | ALEX BEVILACQUA for more info on this topic.

I am a beginner ,so can you help me and tell how can I fix this?

If you select version “2.2.12 or later” from the Atlas Connect UI it will give you the legacy connection string.

Then just copy/paste that into your application and replace the existing mongodb+srv:// version.

I had a similar problem. The sample uri that their website generates, does not do percent encoding for special characters such as @ or '. So just copy pasting that in my js script was not working. When I did that manually, it worked.
See this:
URL encoded