Skip to main content
dns - Node documentation

Usage in Deno

import * as mod from "node:dns";

The node:dns module enables name resolution. For example, use it to look up IP addresses of host names.

Although named for the Domain Name System (DNS), it does not always use the DNS protocol for lookups. lookup uses the operating system facilities to perform name resolution. It may not need to perform any network communication. To perform name resolution the way other applications on the same system do, use lookup.

import dns from 'node:dns';

dns.lookup('example.org', (err, address, family) => {
  console.log('address: %j family: IPv%s', address, family);
});
// address: "93.184.216.34" family: IPv4

All other functions in the node:dns module connect to an actual DNS server to perform name resolution. They will always use the network to perform DNS queries. These functions do not use the same set of configuration files used by lookup (e.g. /etc/hosts). Use these functions to always perform DNS queries, bypassing other name-resolution facilities.

import dns from 'node:dns';

dns.resolve4('archive.org', (err, addresses) => {
  if (err) throw err;

  console.log(`addresses: ${JSON.stringify(addresses)}`);

  addresses.forEach((a) => {
    dns.reverse(a, (err, hostnames) => {
      if (err) {
        throw err;
      }
      console.log(`reverse for ${a}: ${JSON.stringify(hostnames)}`);
    });
  });
});

See the Implementation considerations section for more information.

Classes

Functions

f
getDefaultResultOrder

Get the default value for order in lookup and dnsPromises.lookup().The value could be:

f
getServers

Returns an array of IP address strings, formatted according to RFC 5952,that are currently configured for DNS resolution. A string will include a portsection if a custom port is used.

f
lookup

Resolves a host name (e.g. 'nodejs.org') into the first found A (IPv4) orAAAA (IPv6) record. All option properties are optional. If options is aninteger, then it must be 4 or 6 – if options is 0 or not provided, thenIPv4 and IPv6 addresses are both returned if found.

f
lookupService

Resolves the given address and port into a host name and service usingthe operating system's underlying getnameinfo implementation.

f
promises.getDefaultResultOrder

Get the default value for verbatim in lookup and dnsPromises.lookup().The value could be:

f
promises.getServers

Returns an array of IP address strings, formatted according to RFC 5952,that are currently configured for DNS resolution. A string will include a portsection if a custom port is used.

f
promises.lookup

Resolves a host name (e.g. 'nodejs.org') into the first found A (IPv4) orAAAA (IPv6) record. All option properties are optional. If options is aninteger, then it must be 4 or 6 – if options is not provided, then IPv4and IPv6 addresses are both returned if found.

f
promises.lookupService

Resolves the given address and port into a host name and service usingthe operating system's underlying getnameinfo implementation.

f
promises.resolve

Uses the DNS protocol to resolve a host name (e.g. 'nodejs.org') into an arrayof the resource records. When successful, the Promise is resolved with anarray of resource records. The type and structure of individual results varybased on rrtype:

f
promises.resolve4

Uses the DNS protocol to resolve IPv4 addresses (A records) for the hostname. On success, the Promise is resolved with an array of IPv4addresses (e.g. ['74.125.79.104', '74.125.79.105', '74.125.79.106']).

f
promises.resolve6

Uses the DNS protocol to resolve IPv6 addresses (AAAA records) for the hostname. On success, the Promise is resolved with an array of IPv6addresses.

f
promises.resolveAny

Uses the DNS protocol to resolve all records (also known as ANY or * query).On success, the Promise is resolved with an array containing various types ofrecords. Each object has a property type that indicates the type of thecurrent record. And depending on the type, additional properties will bepresent on the object:

f
promises.resolveCaa

Uses the DNS protocol to resolve CAA records for the hostname. On success,the Promise is resolved with an array of objects containing availablecertification authority authorization records available for the hostname (e.g. [{critical: 0, iodef: 'mailto:pki@example.com'},{critical: 128, issue: 'pki.example.com'}]).

f
promises.resolveCname

Uses the DNS protocol to resolve CNAME records for the hostname. On success,the Promise is resolved with an array of canonical name records available forthe hostname (e.g. ['bar.example.com']).

f
promises.resolveMx

Uses the DNS protocol to resolve mail exchange records (MX records) for the hostname. On success, the Promise is resolved with an array of objectscontaining both a priority and exchange property (e.g.[{priority: 10, exchange: 'mx.example.com'}, ...]).

f
promises.resolveNaptr

Uses the DNS protocol to resolve regular expression-based records (NAPTR records) for the hostname. On success, the Promise is resolved with an arrayof objects with the following properties:

f
promises.resolveNs

Uses the DNS protocol to resolve name server records (NS records) for the hostname. On success, the Promise is resolved with an array of name serverrecords available for hostname (e.g.['ns1.example.com', 'ns2.example.com']).

f
promises.resolvePtr

Uses the DNS protocol to resolve pointer records (PTR records) for the hostname. On success, the Promise is resolved with an array of stringscontaining the reply records.

f
promises.resolveSoa

Uses the DNS protocol to resolve a start of authority record (SOA record) forthe hostname. On success, the Promise is resolved with an object with thefollowing properties:

f
promises.resolveSrv

Uses the DNS protocol to resolve service records (SRV records) for the hostname. On success, the Promise is resolved with an array of objects withthe following properties:

f
promises.resolveTxt

Uses the DNS protocol to resolve text queries (TXT records) for the hostname. On success, the Promise is resolved with a two-dimensional arrayof the text records available for hostname (e.g.[ ['v=spf1 ip4:0.0.0.0 ', '~all' ] ]). Each sub-array contains TXT chunks ofone record. Depending on the use case, these could be either joined together ortreated separately.

f
promises.reverse

Performs a reverse DNS query that resolves an IPv4 or IPv6 address to anarray of host names.

f
promises.setDefaultResultOrder

Set the default value of order in dns.lookup() and [lookup](.././dns/~/lookup). The value could be:

f
promises.setServers

Sets the IP address and port of servers to be used when performing DNSresolution. The servers argument is an array of RFC 5952 formattedaddresses. If the port is the IANA default DNS port (53) it can be omitted.

f
resolve

Uses the DNS protocol to resolve a host name (e.g. 'nodejs.org') into an arrayof the resource records. The callback function has arguments (err, records). When successful, records will be an array of resourcerecords. The type and structure of individual results varies based on rrtype:

f
resolve4

Uses the DNS protocol to resolve a IPv4 addresses (A records) for the hostname. The addresses argument passed to the callback functionwill contain an array of IPv4 addresses (e.g.['74.125.79.104', '74.125.79.105', '74.125.79.106']).

f
resolve6

Uses the DNS protocol to resolve IPv6 addresses (AAAA records) for the hostname. The addresses argument passed to the callback functionwill contain an array of IPv6 addresses.

f
resolveAny

Uses the DNS protocol to resolve all records (also known as ANY or * query).The ret argument passed to the callback function will be an array containingvarious types of records. Each object has a property type that indicates thetype of the current record. And depending on the type, additional propertieswill be present on the object:

f
resolveCaa

Uses the DNS protocol to resolve CAA records for the hostname. The addresses argument passed to the callback functionwill contain an array of certification authority authorization recordsavailable for the hostname (e.g. [{critical: 0, iodef: 'mailto:pki@example.com'}, {critical: 128, issue: 'pki.example.com'}]).

f
resolveCname

Uses the DNS protocol to resolve CNAME records for the hostname. The addresses argument passed to the callback functionwill contain an array of canonical name records available for the hostname (e.g. ['bar.example.com']).

f
resolveMx

Uses the DNS protocol to resolve mail exchange records (MX records) for the hostname. The addresses argument passed to the callback function willcontain an array of objects containing both a priority and exchange property (e.g. [{priority: 10, exchange: 'mx.example.com'}, ...]).

f
resolveNaptr

Uses the DNS protocol to resolve regular expression-based records (NAPTR records) for the hostname. The addresses argument passed to the callback function will contain an array ofobjects with the following properties:

f
resolveNs

Uses the DNS protocol to resolve name server records (NS records) for the hostname. The addresses argument passed to the callback function willcontain an array of name server records available for hostname (e.g. ['ns1.example.com', 'ns2.example.com']).

f
resolvePtr

Uses the DNS protocol to resolve pointer records (PTR records) for the hostname. The addresses argument passed to the callback function willbe an array of strings containing the reply records.

f
resolveSoa

Uses the DNS protocol to resolve a start of authority record (SOA record) forthe hostname. The address argument passed to the callback function willbe an object with the following properties:

f
resolveSrv

Uses the DNS protocol to resolve service records (SRV records) for the hostname. The addresses argument passed to the callback function willbe an array of objects with the following properties:

f
resolveTxt

Uses the DNS protocol to resolve text queries (TXT records) for the hostname. The records argument passed to the callback function is atwo-dimensional array of the text records available for hostname (e.g.[ ['v=spf1 ip4:0.0.0.0 ', '~all' ] ]). Each sub-array contains TXT chunks ofone record. Depending on the use case, these could be either joined together ortreated separately.

f
reverse

Performs a reverse DNS query that resolves an IPv4 or IPv6 address to anarray of host names.

f
setDefaultResultOrder

Set the default value of order in lookup and dnsPromises.lookup().The value could be:

f
setServers

Sets the IP address and port of servers to be used when performing DNSresolution. The servers argument is an array of RFC 5952 formattedaddresses. If the port is the IANA default DNS port (53) it can be omitted.

Interfaces

I
AnyAaaaRecord
No documentation available
I
AnyARecord
No documentation available
I
AnyCnameRecord
No documentation available
I
AnyMxRecord
No documentation available
I
AnyNaptrRecord
No documentation available
I
AnyNsRecord
No documentation available
I
AnyPtrRecord
No documentation available
I
AnySoaRecord
No documentation available
I
AnySrvRecord
No documentation available
I
AnyTxtRecord
No documentation available
I
LookupAddress
No documentation available
I
LookupAllOptions
No documentation available
I
LookupOneOptions
No documentation available
I
LookupOptions
No documentation available
I
MxRecord
No documentation available
I
RecordWithTtl
No documentation available
I
ResolveOptions
No documentation available
I
ResolverOptions
No documentation available
I
ResolveWithTtlOptions
No documentation available
I
SrvRecord
No documentation available

Namespaces

N
promises

The dns.promises API provides an alternative set of asynchronous DNS methodsthat return Promise objects rather than using callbacks. The API is accessiblevia import { promises as dnsPromises } from 'node:dns' or import dnsPromises from 'node:dns/promises'.

Type Aliases

T
AnyRecord
No documentation available
T
AnyRecordWithTtl
No documentation available

Variables

v
ADDRCONFIG

Limits returned address types to the types of non-loopback addresses configured on the system. For example, IPv4 addresses areonly returned if the current system has at least one IPv4 address configured.

v
ADDRGETNETWORKPARAMS
No documentation available
v
ALL

If dns.V4MAPPED is specified, return resolved IPv6 addresses aswell as IPv4 mapped IPv6 addresses.

v
BADFAMILY
No documentation available
v
BADFLAGS
No documentation available
v
BADHINTS
No documentation available
v
BADNAME
No documentation available
v
BADQUERY
No documentation available
v
BADRESP
No documentation available
v
BADSTR
No documentation available
v
CANCELLED
No documentation available
v
CONNREFUSED
No documentation available
v
DESTRUCTION
No documentation available
v
EOF
No documentation available
v
FILE
No documentation available
v
FORMERR
No documentation available
v
LOADIPHLPAPI
No documentation available
v
NODATA
No documentation available
v
NOMEM
No documentation available
v
NONAME
No documentation available
v
NOTFOUND
No documentation available
v
NOTIMP
No documentation available
v
NOTINITIALIZED
No documentation available
v
promises.ADDRGETNETWORKPARAMS
No documentation available
v
promises.BADFAMILY
No documentation available
v
promises.BADFLAGS
No documentation available
v
promises.BADHINTS
No documentation available
v
promises.BADNAME
No documentation available
v
promises.BADQUERY
No documentation available
v
promises.BADRESP
No documentation available
v
promises.BADSTR
No documentation available
v
promises.CANCELLED
No documentation available
v
promises.CONNREFUSED
No documentation available
v
promises.DESTRUCTION
No documentation available
v
promises.EOF
No documentation available
v
promises.FILE
No documentation available
v
promises.FORMERR
No documentation available
v
promises.LOADIPHLPAPI
No documentation available
v
promises.NODATA
No documentation available
v
promises.NOMEM
No documentation available
v
promises.NONAME
No documentation available
v
promises.NOTFOUND
No documentation available
v
promises.NOTIMP
No documentation available
v
promises.NOTINITIALIZED
No documentation available
v
promises.REFUSED
No documentation available
v
promises.SERVFAIL
No documentation available
v
promises.TIMEOUT
No documentation available
v
REFUSED
No documentation available
v
SERVFAIL
No documentation available
v
TIMEOUT
No documentation available
v
V4MAPPED

If the IPv6 family was specified, but no IPv6 addresses were found, then return IPv4 mapped IPv6 addresses. It is not supportedon some operating systems (e.g. FreeBSD 10.1).