An independent resolver for DNS requests.

Creating a new resolver uses the default server settings. Setting the servers used for a resolver using resolver.setServers() does not affect other resolvers:

import { promises } from 'node:dns';
const resolver = new promises.Resolver();
resolver.setServers(['4.4.4.4']);

// This request will use the server at 4.4.4.4, independent of global settings.
resolver.resolve4('example.org').then((addresses) => {
// ...
});

// Alternatively, the same code can be written using async-await style.
(async function() {
const addresses = await resolver.resolve4('example.org');
})();

The following methods from the dnsPromises API are available:

  • resolver.getServers()
  • resolver.resolve()
  • resolver.resolve4()
  • resolver.resolve6()
  • resolver.resolveAny()
  • resolver.resolveCaa()
  • resolver.resolveCname()
  • resolver.resolveMx()
  • resolver.resolveNaptr()
  • resolver.resolveNs()
  • resolver.resolvePtr()
  • resolver.resolveSoa()
  • resolver.resolveSrv()
  • resolver.resolveTxt()
  • resolver.reverse()
  • resolver.setServers()

v10.6.0

Constructors

Properties

getServers: (() => string[])

Type declaration

    • (): string[]
    • Returns an array of IP address strings, formatted according to RFC 5952, that are currently configured for DNS resolution. A string will include a port section if a custom port is used.

      [
      '4.4.4.4',
      '2001:4860:4860::8888',
      '4.4.4.4:1053',
      '[2001:4860:4860::8888]:1053',
      ]

      Returns string[]

      v10.6.0

resolve: {
    (hostname: string): Promise<string[]>;
    (hostname: string, rrtype:
        | "A"
        | "AAAA"
        | "NS"
        | "PTR"
        | "CNAME"): Promise<string[]>;
    (hostname: string, rrtype: "ANY"): Promise<AnyRecord[]>;
    (hostname: string, rrtype: "CAA"): Promise<CaaRecord[]>;
    (hostname: string, rrtype: "MX"): Promise<MxRecord[]>;
    (hostname: string, rrtype: "NAPTR"): Promise<NaptrRecord[]>;
    (hostname: string, rrtype: "SOA"): Promise<SoaRecord>;
    (hostname: string, rrtype: "SRV"): Promise<SrvRecord[]>;
    (hostname: string, rrtype: "TLSA"): Promise<TlsaRecord[]>;
    (hostname: string, rrtype: "TXT"): Promise<string[][]>;
    (hostname: string, rrtype: string): Promise<
        | string[]
        | CaaRecord[]
        | MxRecord[]
        | NaptrRecord[]
        | SoaRecord
        | SrvRecord[]
        | TlsaRecord[]
        | string[][]
        | AnyRecord[]>;
}

Type declaration

resolve4: {
    (hostname: string): Promise<string[]>;
    (hostname: string, options: ResolveWithTtlOptions): Promise<RecordWithTtl[]>;
    (hostname: string, options: ResolveOptions): Promise<string[] | RecordWithTtl[]>;
}

Type declaration

resolve6: {
    (hostname: string): Promise<string[]>;
    (hostname: string, options: ResolveWithTtlOptions): Promise<RecordWithTtl[]>;
    (hostname: string, options: ResolveOptions): Promise<string[] | RecordWithTtl[]>;
}

Type declaration

resolveAny: ((hostname: string) => Promise<AnyRecord[]>)

Type declaration

    • (hostname): Promise<AnyRecord[]>
    • 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 of records. Each object has a property type that indicates the type of the current record. And depending on the type, additional properties will be present on the object:

      Here is an example of the result object:

      [ { type: 'A', address: '127.0.0.1', ttl: 299 },
      { type: 'CNAME', value: 'example.com' },
      { type: 'MX', exchange: 'alt4.aspmx.l.example.com', priority: 50 },
      { type: 'NS', value: 'ns1.example.com' },
      { type: 'TXT', entries: [ 'v=spf1 include:_spf.example.com ~all' ] },
      { type: 'SOA',
      nsname: 'ns1.example.com',
      hostmaster: 'admin.example.com',
      serial: 156696742,
      refresh: 900,
      retry: 900,
      expire: 1800,
      minttl: 60 } ]

      Parameters

      • hostname: string

      Returns Promise<AnyRecord[]>

      v10.6.0

resolveCaa: ((hostname: string) => Promise<CaaRecord[]>)

Type declaration

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

      Parameters

      • hostname: string

      Returns Promise<CaaRecord[]>

      v15.0.0, v14.17.0

resolveCname: ((hostname: string) => Promise<string[]>)

Type declaration

    • (hostname): Promise<string[]>
    • 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 for the hostname (e.g. ['bar.example.com']).

      Parameters

      • hostname: string

      Returns Promise<string[]>

      v10.6.0

resolveMx: ((hostname: string) => Promise<MxRecord[]>)

Type declaration

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

      Parameters

      • hostname: string

      Returns Promise<MxRecord[]>

      v10.6.0

resolveNaptr: ((hostname: string) => Promise<NaptrRecord[]>)

Type declaration

    • (hostname): Promise<NaptrRecord[]>
    • Uses the DNS protocol to resolve regular expression-based records (NAPTR records) for the hostname. On success, the Promise is resolved with an array of objects with the following properties:

      • flags
      • service
      • regexp
      • replacement
      • order
      • preference
      {
      flags: 's',
      service: 'SIP+D2U',
      regexp: '',
      replacement: '_sip._udp.example.com',
      order: 30,
      preference: 100
      }

      Parameters

      • hostname: string

      Returns Promise<NaptrRecord[]>

      v10.6.0

resolveNs: ((hostname: string) => Promise<string[]>)

Type declaration

    • (hostname): Promise<string[]>
    • 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 server records available for hostname (e.g.['ns1.example.com', 'ns2.example.com']).

      Parameters

      • hostname: string

      Returns Promise<string[]>

      v10.6.0

resolvePtr: ((hostname: string) => Promise<string[]>)

Type declaration

    • (hostname): Promise<string[]>
    • Uses the DNS protocol to resolve pointer records (PTR records) for the hostname. On success, the Promise is resolved with an array of strings containing the reply records.

      Parameters

      • hostname: string

      Returns Promise<string[]>

      v10.6.0

resolveSoa: ((hostname: string) => Promise<SoaRecord>)

Type declaration

    • (hostname): Promise<SoaRecord>
    • Uses the DNS protocol to resolve a start of authority record (SOA record) for the hostname. On success, the Promise is resolved with an object with the following properties:

      • nsname
      • hostmaster
      • serial
      • refresh
      • retry
      • expire
      • minttl
      {
      nsname: 'ns.example.com',
      hostmaster: 'root.example.com',
      serial: 2013101809,
      refresh: 10000,
      retry: 2400,
      expire: 604800,
      minttl: 3600
      }

      Parameters

      • hostname: string

      Returns Promise<SoaRecord>

      v10.6.0

resolveSrv: ((hostname: string) => Promise<SrvRecord[]>)

Type declaration

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

      • priority
      • weight
      • port
      • name
      {
      priority: 10,
      weight: 5,
      port: 21223,
      name: 'service.example.com'
      }

      Parameters

      • hostname: string

      Returns Promise<SrvRecord[]>

      v10.6.0

resolveTlsa: ((hostname: string) => Promise<TlsaRecord[]>)

Type declaration

    • (hostname): Promise<TlsaRecord[]>
    • Uses the DNS protocol to resolve certificate associations (TLSA records) for the hostname. On success, the Promise is resolved with an array of objectsAdd commentMore actions with these properties:

      • certUsage
      • selector
      • match
      • data
      {
      certUsage: 3,
      selector: 1,
      match: 1,
      data: [ArrayBuffer]
      }

      Parameters

      • hostname: string

      Returns Promise<TlsaRecord[]>

      v22.15.0

resolveTxt: ((hostname: string) => Promise<string[][]>)

Type declaration

    • (hostname): Promise<string[][]>
    • Uses the DNS protocol to resolve text queries (TXT records) for the hostname. On success, the Promise is resolved with a two-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 of one record. Depending on the use case, these could be either joined together or treated separately.

      Parameters

      • hostname: string

      Returns Promise<string[][]>

      v10.6.0

reverse: ((ip: string) => Promise<string[]>)

Type declaration

    • (ip): Promise<string[]>
    • Performs a reverse DNS query that resolves an IPv4 or IPv6 address to an array of host names.

      On error, the Promise is rejected with an Error object, where err.code is one of the DNS error codes.

      Parameters

      • ip: string

      Returns Promise<string[]>

      v10.6.0

setServers: ((servers: readonly string[]) => void)

Type declaration

    • (servers): void
    • Sets the IP address and port of servers to be used when performing DNS resolution. The servers argument is an array of RFC 5952 formatted addresses. If the port is the IANA default DNS port (53) it can be omitted.

      dnsPromises.setServers([
      '4.4.4.4',
      '[2001:4860:4860::8888]',
      '4.4.4.4:1053',
      '[2001:4860:4860::8888]:1053',
      ]);

      An error will be thrown if an invalid address is provided.

      The dnsPromises.setServers() method must not be called while a DNS query is in progress.

      This method works much like resolve.conf. That is, if attempting to resolve with the first server provided results in a NOTFOUND error, the resolve() method will not attempt to resolve with subsequent servers provided. Fallback DNS servers will only be used if the earlier ones time out or result in some other error.

      Parameters

      • servers: readonly string[]

        array of RFC 5952 formatted addresses

      Returns void

      v10.6.0

Methods

  • Cancel all outstanding DNS queries made by this resolver. The corresponding callbacks will be called with an error with code ECANCELLED.

    Returns void

    v8.3.0

  • The resolver instance will send its requests from the specified IP address. This allows programs to specify outbound interfaces when used on multi-homed systems.

    If a v4 or v6 address is not specified, it is set to the default and the operating system will choose a local address automatically.

    The resolver will use the v4 local address when making requests to IPv4 DNS servers, and the v6 local address when making requests to IPv6 DNS servers. The rrtype of resolution requests has no impact on the local address used.

    Parameters

    • Optionalipv4: string

      A string representation of an IPv4 address.

    • Optionalipv6: string

      A string representation of an IPv6 address.

    Returns void

    v15.1.0, v14.17.0