Skip to content

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

Key points

  • A full URL passed as host is a common bug.
  • EAI_AGAIN is temporary, ENOTFOUND is usually definitive.
  • dns.lookup uses the libuv thread pool, which can be exhausted.
  • Containers have their own DNS.

What it means

getaddrinfo is the operating system's name lookup. ENOTFOUND means the name did not resolve, and EAI_AGAIN means the resolver had a temporary failure, such as a DNS timeout. The hostname appears in the error, so read it carefully for typos and stray protocol prefixes.

Common causes

  • https:// included in a host option.
  • An unset environment variable, leading to undefined or an empty hostname.
  • Service names that only exist inside Docker or Kubernetes.
  • CoreDNS overloaded, or ndots causing many extra lookups.
  • libuv thread pool exhaustion, which delays lookups.

Diagnose it

Test the same lookup in the same environment as the failing code.

bash
# 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-service

Fixes

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.

Frequently asked questions

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.

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.