node:dns
Node.js dns module, resolvable via both ESM import and CommonJS require, plus the promise API at node:dns/promises. Both are registered built-ins backed by a native resolver.
Functions
| Function | Description |
|---|---|
lookup(hostname[, options], callback) | Resolve a hostname to an address using the system resolver order. |
lookupService(address, port, callback) | Reverse-resolve an address and port to a hostname and service. |
getDefaultResultOrder() | Return the current default address ordering. |
setDefaultResultOrder(order) | Set the default address ordering (ipv4first / verbatim). |
reverse(ip, callback) | Reverse-resolve an IP address to an array of hostnames. |
Record resolution
Theresolve* family queries specific DNS record types:
| Function | Record type |
|---|---|
resolve4(hostname, cb) | A |
resolve6(hostname, cb) | AAAA |
resolveCname(hostname, cb) | CNAME |
resolveNs(hostname, cb) | NS |
resolvePtr(hostname, cb) | PTR |
resolveMx(hostname, cb) | MX |
resolveSrv(hostname, cb) | SRV |
resolveCaa(hostname, cb) | CAA |
resolveNaptr(hostname, cb) | NAPTR |
resolveTlsa(hostname, cb) | TLSA |
resolveTxt(hostname, cb) | TXT |
resolveSoa(hostname, cb) | SOA |
resolveAny(hostname, cb) | multiple record types (ANY) |
The Resolver class
new dns.Resolver([options]) creates an independent resolver exposing the same resolve* methods, so options can be scoped per instance rather than shared with the default resolver. options accepts timeout (in milliseconds) and tries. resolver.cancel() cancels all outstanding queries on that resolver.
> resolver.setLocalAddress() is not implemented.
Error codes
node:dns exports the standard DNS error-code constants — such as NODATA, NOTFOUND, SERVFAIL, REFUSED, and CANCELLED — for comparing against a failed query's err.code. These constants are exported from node:dns only, not from node:dns/promises.
Promises API
node:dns/promises exposes the same surface as promise-returning functions instead of callbacks, as in Node:
js
import dns from "node:dns/promises";
const addresses = await dns.resolve4("example.com");