Node.js dns - lookup vs resolve, promises API and custom resolvers
Answer: Use dns.lookup() to resolve a host name the way other applications on the same system do, including /etc/hosts. Use dns.resolve4(), dns.resolve() and dns.reverse() when you need actual DNS queries on the network. The node:dns module enables name resolution, but lookup() does not always use the DNS protocol.
Examples
import dns from 'node:dns/promises';
const { address, family } = await dns.lookup('example.org');
const all = await dns.lookup('example.org', { all: true });
const v4 = await dns.resolve4('archive.org'); // DNS query
const mx = await dns.resolveMx('example.org');
const names = await dns.reverse(v4[0]);
// Independent resolver with its own servers
const resolver = new dns.Resolver();
resolver.setServers(['1.1.1.1']);
await resolver.resolve4('example.org');
lookup vs resolve
| | dns.lookup() | dns.resolve*() / dns.reverse() | |---|---|---| | Uses | operating system facilities (getaddrinfo) | an actual DNS server | | Uses /etc/hosts configuration | uses it | does not use it | | Network | may not need any network communication | always perform a DNS query on the network | | Threadpool | runs on libuv's threadpool | does not use libuv's threadpool |
Rules
- In practice
dns.lookup()resolves a name almost always the same way the ping command does. - Though
dns.lookup()is asynchronous from JavaScript's perspective, it is implemented as a synchronous call to getaddrinfo(3) on libuv's threadpool, which can hurt performance; see UV_THREADPOOL_SIZE. - Various networking APIs call
dns.lookup()internally to resolve host names. new Resolver()creates an independent resolver for DNS requests. Setting the servers used for one resolver does not affect other resolvers.- Changing nsswitch.conf or resolv.conf changes
lookup()for all other programs on the same operating system.
See also
Claims
- id: C1 claim: lookup uses OS facilities and may skip the network. source: S1 evidence: "dns.lookup() uses the operating system facilities to perform name resolution."
- id: C2 claim: resolve functions always query DNS on the network. source: S1 evidence: "they always perform a DNS query on the network."
- id: C3 claim: lookup runs getaddrinfo synchronously on the threadpool. source: S2 evidence: "it is implemented as a synchronous call to getaddrinfo(3) that runs"
Sources
- [S1] DNS. Node.js API docs. https://nodejs.org/api/dns.html . License: MIT. Accessed 2026-11-18.
- [S2] doc/api/dns.md. nodejs/node (GitHub). https://github.com/nodejs/node/blob/main/doc/api/dns.md . License: MIT. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Node.js API docs, DNS; nodejs/node (GitHub), doc/api/dns.md. MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Node.js API docs, DNS; nodejs/node (GitHub), doc/api/dns.md. MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors.
Cite this
iinventedlight. 2026. "Node.js dns - lookup vs resolve, promises API and custom resolvers." iinventedlight Library, iil:note:kaszhtr7, version of 2026-11-18. https://iinventedlight.com/library/notes/node-dns/. License: CC-BY-4.0.
@misc{iil_note_kaszhtr7,
author = {{iinventedlight}},
title = {{Node.js dns - lookup vs resolve, promises API and custom resolvers}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/node-dns/},
note = {Permanent ID iil:note:kaszhtr7. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- node-dns.table.json (table; CC-BY-4.0)
- node-dns.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/node-dns.md.