'Why "dns.resolve" of NodeJS hangs up when "localhost" is the parameter?
I can understand if I use the dns.resolve() improperly, but hanging up is a starange behaviour. Should not dns.resolve() throw the error if I doing something wrong?
const dns = require("dns").promises;
dns.resolve("localhost").
then(
domains => {
console.log("===========");
console.log(domains);
}
).
catch((error) => { console.error(error); });
My version of Node.js is 16.13.0.
Solution 1:[1]
I think this is explained here: https://nodejs.org/api/dns.html#dnsresolve-dnsresolve-and-dnsreverse
dns.resolve(), dns.resolve*() and dns.reverse() They do not use the same set of configuration files than what dns.lookup() uses. For instance, they do not use the configuration from /etc/hosts.
On my MacOS dns.resolve() correctly returns 127.0.0.1 when I use the default DNS server, when I use 8.8.8.8 it throws this error but does not hang:
Error: queryA ENOTFOUND localhost
at QueryReqWrap.onresolve [as oncomplete] (internal/dns/promises.js:172:17) {
errno: undefined,
code: 'ENOTFOUND',
syscall: 'queryA',
hostname: 'localhost'
}
So my guess the problem is caused by a configuration on your system, this GitHub issues contains more information: https://github.com/nodejs/help/issues/2163
Using dns.lookup() correctly returns 127.0.0.1 with default and 8.8.8.8, try it and see how it works.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrei |
