jsrosetta

I/O

DNS Lookups

How Node.js's node:dns/promises compares to the net package (Go), hickory-resolver (Rust), getaddrinfo (Swift, A/AAAA only), and JNDI (Java) for DNS lookups.

Minimum versions
Node.js ≥ 15Go ≥ 1.9Rust ≥ 1.88Swift ≥ 3.0Java ≥ 25
Verified on
Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1

Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.

DNS lookups (NS, A/AAAA, MX, TXT) are available in the standard library of both Node.js and Go, with no third-party dependency needed. Rust's std can only look up A/AAAA through ToSocketAddrs (using the OS resolver) — NS/MX/TXT need the hickory-resolver crate, a pure-Rust, in-process DNS resolver that doesn't go through libc. Swift/Foundation is even more limited: only A/AAAA (via getaddrinfo, since Host is to-be-deprecated and macOS-only), with no API at all for NS/MX/TXT. Java has InetAddress for A/AAAA and JNDI (com.sun.jndi.dns.DnsContextFactory) for every other record type — an old technique, but one that still works fine on modern JDKs.

Looking up NS, IP, MX, and TXT records

import dns from 'node:dns/promises'
 
const ns = await dns.resolveNs('google.com')
console.log(ns)
 
const ips = await dns.resolve4('google.com')
console.log(ips)
 
const mx = await dns.resolveMx('google.com')
console.log(mx)
 
const txt = await dns.resolveTxt('google.com')
console.log(txt)
# DNS answers vary between lookups; TXT records trimmed with …
# (Node.js, Go, Rust, and Java can look up NS/A/MX/TXT; Swift above only gets A/AAAA)
ns1.google.com.
[142.251.12.138 142.251.12.102 …]
smtp.google.com. 10
[v=spf1 include:_spf.google.com ~all …]

Changing the DNS resolver server

dns.setServers(['1.1.1.1'])
console.log(dns.getServers())
 
const ns2 = await dns.resolveNs('google.com')
console.log(ns2)

Reference: github.com/miguelmota/golang-for-nodejs-developers#dns