Troubleshooting guide
ENOTFOUND getaddrinfo: how to fix DNS errors in Node.js
Node.js asked the operating system to resolve a hostname and got no answer. Often the hostname is simply malformed.
3 min read11 sectionsUpdated
On this page
# Resolve using Node's own path
node -e "require('dns').lookup('api.example.com', (e, a) => console.log(e || a))"
# Inside the container
docker exec app cat /etc/resolv.conf
docker exec app getent hosts api.example.com
# Kubernetes DNS health
kubectl -n kube-system logs -l k8s-app=kube-dns --tail=20
kubectl exec my-pod -- nslookup my-serviceFixes
Use only the hostname in host, and let new URL() parse any full address. Validate required environment variables when the application starts. Use the right service name for your network. Retry EAI_AGAIN with backoff, but not ENOTFOUND for a wrong name. Reduce lookups by reusing connections with keep-alive, and in Kubernetes use a fully-qualified name with a trailing dot or lower ndots.
Why DNS gets slow under load
dns.lookup runs in a libuv thread pool of four threads by default, shared with file system and some crypto calls. If those are busy, DNS lookups wait, and timeouts appear that look like network failures. Raising UV_THREADPOOL_SIZE helps, and so does reusing connections so that lookups are rare. Inside Kubernetes, the default ndots:5 means a name with few dots is tried with every search domain first, multiplying queries against CoreDNS. NodeLocal DNSCache reduces that load and avoids conntrack races.
Catch it automatically
DNS monitoring confirms your external names resolve, and APM shows errors grouped by the dependency that failed. Failures that appear across many services at once suggest cluster DNS rather than one service's configuration.
What is the difference between ENOTFOUND and EAI_AGAIN?
ENOTFOUND means the name could not be found. EAI_AGAIN means a temporary resolver failure.
Why is the hostname undefined?
An environment variable was probably unset.
Does Node cache DNS?
No, Node does not cache lookups by default. Reuse connections instead.
Check it now, free
- DNS lookup: A, AAAA, CNAME, MX, TXT, NS, SOA and CAA records.
Related reading
- Cloudflare error 1016 "Origin DNS error": how to fix it: Fix Cloudflare error 1016 Origin DNS error: CNAME records pointing to names that do not resolve, deleted origins and load balancer pools, with checks.
- DNS_PROBE_FINISHED_NXDOMAIN: fix it for your domain: What DNS_PROBE_FINISHED_NXDOMAIN means, how to check your domain and DNS records, the usual causes such as expiry and typos, and how to monitor DNS.
- DNS SERVFAIL: causes and how to fix it: Fix DNS SERVFAIL responses: broken DNSSEC chains, unreachable or lame nameservers and misconfigured delegation, with dig commands that isolate each cause.
- Mixed content errors: how to find and fix them: Fix mixed content warnings on HTTPS pages: find http:// images, scripts and API calls, update CMS URLs and add a Content-Security-Policy upgrade directive.
- DNS_PROBE_FINISHED_NO_INTERNET: how to fix it: Fix DNS_PROBE_FINISHED_NO_INTERNET in Chrome: check the connection, restart the router, flush DNS, reset network settings and switch DNS servers.
- Error: socket hang up: Why Node.js reports "socket hang up", from servers closing connections and timeouts to proxies and TLS problems, with steps to find the cause.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.