Networking

dns.lookup() versus DNS record resolution

Name resolution turns a hostname into information needed for a connection. Node offers `dns.lookup()` for OS name resolution and `dns.resolve*()` for DNS record queries. A resolution success is not a connection success: the target may refuse the connection or fail to respond. `lookup` can consult local hosts configuration and OS name services; `resolve4` asks for DNS A records through the resolver. `lookup` can use the libuv pool for OS getaddrinfo. Use `all: true` when you need the full returned address list, and remember that address order and IPv6 availability depend on the environment. Error codes such as ENOTFOUND, EAI_AGAIN, ECONNREFUSED and ETIMEDOUT are diagnostic clues, not definitive root-cause proofs. Bound retries and distinguish a DNS error from a transport or application error. Do not log private hostnames or assume DNS answers stay fixed. Do not replace a failed hostname with an arbitrary IP or disable TLS verification. Diagnose resolution, reachability and certificate identity separately. ENOTFOUND does not prove a domain never exists. Sources: https://nodejs.org/download/release/v22.16.0/docs/api/dns.html.

Formal Syntax & Structure

nodejs syntaxnodejs
dns.lookup(hostname[, options], callback); dns.resolve4(hostname[, options], callback)

Working Example

Example Snippetnodejs
import { lookup } from 'node:dns/promises';const addresses=await lookup('127.0.0.1',{all:true});console.log(addresses[0].address,addresses[0].family);
Node.js Reference