Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

getaddrinfo ENOTFOUND in Node.js: why the host name does not resolve, and the fix

Error: getaddrinfo ENOTFOUND means Node asked the system for the address of a host name and got none. The name at the end of the message is the one your app really used: a typo or https:// left in a host field, the word undefined from an unset variable, a Docker Compose service name or a host that exists only on your laptop, or another platform's internal address. getaddrinfo EAI_AGAIN is a temporary DNS failure instead. Print the host, check that it resolves where the app runs, and fix the variable that holds it.

By Livops team, the people who build and run LivopsUpdated

The message, from any Node client that opens a connection by host name, here net.connect() on Node 24:

Error: getaddrinfo ENOTFOUND db
    at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:122:26) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'db'
}

The last word changes with your app: getaddrinfo ENOTFOUND undefined, getaddrinfo ENOTFOUND https://db.example.com, getaddrinfo ENOTFOUND redis. A library may wrap it in its own message, as fetch does with TypeError: fetch failed and the error above in cause, but the line that matters is the same. Its sibling is getaddrinfo EAI_AGAIN api.example.com. This page explains what the lookup is telling you, how to print the host your app really uses, the causes from the most common, and the fix for each.

What does the error mean?

The name did not resolve, before any connection

When a Node client connects to a host name, net looks the name up with dns.lookup() by default[3]. dns.lookup() calls the system’s getaddrinfo()[2] and uses the same operating system facilities as most other programs, so it resolves a name almost always the same way the ping command does[2]. Its settings come from files such as nsswitch.conf and resolv.conf, and, unlike Node’s dns.resolve*() functions, it reads /etc/hosts[2].

ENOTFOUND is the code Node gives a failure of EAI_NODATA or EAI_NONAME[1][4]: in getaddrinfo()’s words, the host exists but has no addresses, or the node is not known at all[6]. Node builds the message as the system call, the code and the host name, and keeps the name in err.hostname[4], so getaddrinfo ENOTFOUND db means: the lookup of db returned no address. The Node docs add that ENOTFOUND is also set when the lookup fails in other ways, such as no free file descriptors[2], but in a deploy it is nearly always the name.

How it differs from the errors next to it

The error says What happened
getaddrinfo ENOTFOUND <host> The name has no address where the app runs[1]
getaddrinfo EAI_AGAIN <host> The name server returned a temporary failure; a retry may succeed[6][4]
connect ECONNREFUSED <ip>:<port> The name resolved, and nothing listened on that port[1]; see ECONNREFUSED
connect ETIMEDOUT <ip>:<port> The name resolved, and the other side did not respond in time[1]
querySrv ENOTFOUND _mongodb._tcp... A mongodb+srv:// lookup; see MongoServerSelectionError

First, print the host the app really uses

The host in the message is already the answer to “what did my code use”. To see where it came from, print the variable the client reads, without the password:

node -e 'console.log(new URL(process.env.DATABASE_URL).hostname)'
node -e 'console.log(JSON.stringify(process.env.REDIS_HOST))'

Run it where the app runs: in the container, on the server, in the platform’s shell. undefined or an empty string means the variable is not set there. Then ask the system to resolve that name, from the same place, the way the app will:

node -e 'require("dns").lookup(process.argv[1], (e, a) => console.log(e ? e.code : a))' db

It prints the address, or the same ENOTFOUND your app got. Using dns.lookup() here matters: it follows the same files the app does, /etc/hosts included, where a DNS-only tool would not[2].

Causes, most common first

1. The variable is not set: ENOTFOUND undefined

const res = await fetch(`http://${process.env.API_HOST}/health`)
// TypeError: fetch failed, cause: getaddrinfo ENOTFOUND undefined

Inside a template string, undefined becomes the text "undefined", and null becomes "null"[9]. So an unset variable does not fail where it is read; it becomes a host name that resolves nowhere. The same happens with a placeholder copied from .env.example (your-db-host, db.example.local) that nobody replaced.

Fix: set the variable where the app runs, under the exact name the code reads, and restart the app: a running process does not pick up new values. Then make a missing value fail at startup, with its name, instead of becoming a host:

const host = process.env.API_HOST
if (!host) throw new Error('API_HOST is not set')

A .env file on your laptop is not on the server unless you put it there, and it is usually kept out of the repository; the values belong in the platform’s variables.

2. A scheme, port or path in a host field

A host option takes a host name[3], not a URL. Everything you put there is looked up as one name:

new pg.Pool({ host: 'https://db.example.com' })
// getaddrinfo ENOTFOUND https://db.example.com

The usual mistakes are https:// or postgres:// in a DB_HOST variable, db.example.com:5432 in a host field that has its own port option, a trailing / or a space pasted with the value, and a URL pasted twice (https://https://api...). Fix: put only the name in the host field, db.example.com, and the port in its own option. Or give the client the whole URL in the option meant for it, such as connectionString for node-postgres[5], and drop the separate host.

3. A name that exists only in Docker Compose or on your machine

In Docker Compose, each service’s container joins the project’s default network and is reachable at a host name equal to the service name: a web service connects to postgres://db:5432[8]. That name is answered by Compose’s own DNS, for containers on that network only. Run the same code anywhere else, a server, a platform, a CI job, or npm start on the host machine itself, and db does not resolve. A name you added to /etc/hosts on your laptop, or one your office network resolves, behaves the same way: dns.lookup() on your laptop finds it, and the server’s does not[2].

Fix: use the address the database really has where the app runs. On a platform it is in the variable the platform writes, usually DATABASE_URL, REDIS_URL or MONGO_URL. Keep db for your Compose file only, by setting the variable there rather than hard-coding the name.

4. Another platform’s internal address

A private address works only inside the network that issued it. On Railway, for example, a service is reachable at SERVICE_NAME.railway.internal, and each environment has its own isolated network[10]. Copy such a value into a project on another host, or call it from production when it belongs to staging, and the name resolves nowhere. The same goes for a Livops database’s host, postgres, used from outside its project (below).

Fix: take the address from the platform where the app now runs. If the other service stays where it is, use its public address, if it has one.

5. A typo, or a name that does not exist yet

ENOTFOUND covers a host that is not known at all[6]: a misspelt domain, a subdomain whose DNS record was never created, a cluster host that was shortened when copied. Check the name letter by letter against the provider’s dashboard and resolve it with the dns.lookup line above. If it fails from your laptop too, the name itself is wrong; no code change will make it resolve.

6. EAI_AGAIN: the lookup failed for now

Error: getaddrinfo EAI_AGAIN api.example.com

EAI_AGAIN means the name server returned a temporary failure, and the man page’s advice is to try again later[6]; Node passes the code on under its own name[4]. The name may be fine. The answer has to come from the name servers listed in /etc/resolv.conf, which the resolver queries in order, waiting 5 seconds for each by default[7], so the place to look is DNS and the network of the machine the app runs on, not the host name itself.

Fix: check the same lookup again from the same place. If it now succeeds, retry the first connection a few times with a pause instead of crashing on the first failure. If it keeps failing, look at /etc/resolv.conf where the app runs and whether that machine can reach its name servers.

On Livops

  • Database addresses are written for you. When you add PostgreSQL, MySQL, Redis or MongoDB under Databases, its address is written as variables to every service in the environment, including services added later: DATABASE_URL and PGHOST…, MYSQL_URL, REDIS_URL, MONGO_URL. Read those, and the host is right by construction. See Databases.
  • The host is an internal name. A PostgreSQL URL looks like postgresql://app:<password>@postgres:5432/app; the host postgres (or redis …) is reachable only from services in the project, and there is no public port. From your laptop, use Connect from your computer on the database page, an SSH tunnel to localhost.
  • Addresses of your other services. Instead of typing another service’s address, a variable can reference it: the platform fills in that service’s URL, host or port. References work only within their own environment, so a service in staging cannot see one in production. See Variables.
  • New values need a deploy. A variable takes effect on the next deploy; restart the service to apply it now.
  • The build has no database. Code that connects to the database while building, a migration in the build command for example, belongs in the pre-deploy command or at runtime.
  • Where it shows. An app that cannot reach its host when it starts fails the deploy as start_failed; its last lines are on the Logs page, and a web app’s previous version keeps serving. A missing variable is the most common cause there; see The app did not start.

A Node app costs 39 credits a month; a database is free at 500 MB, one per team, and 1 GB is 9 credits a month. See Pricing.

Questions

What does getaddrinfo ENOTFOUND mean?

Node asked the operating system for the IP address of a host name, the way ping would, and the system answered that the name has no address. Nothing was sent to the server yet. The name at the end of the message is the exact host your code used, so read it first.

Why does the error say ENOTFOUND undefined?

Your code built the host or the URL from an environment variable that is not set. JavaScript turns undefined into the text undefined inside a template string, so Node looked up a host literally called undefined. Set the variable where the app runs, under the name the code reads, and restart the app.

What is the difference between ENOTFOUND and EAI_AGAIN?

ENOTFOUND is a definite answer: the name has no address. EAI_AGAIN means the name server returned a temporary failure, so the lookup could succeed on a retry. A wrong name gives ENOTFOUND every time; EAI_AGAIN points at the DNS servers or the network of the machine the app runs on.

Is ENOTFOUND the same as ECONNREFUSED?

No. ENOTFOUND happens before any connection: the name did not resolve. ECONNREFUSED comes after a successful lookup, when the host answered and nothing was listening on that port. The fixes are different: the first is about the address, the second about the server behind it.

Why does my database host work in Docker Compose but not after deploy?

Compose gives each service a host name equal to its service name, such as db, and that name resolves only for containers on the same Compose network. On another machine nobody knows db. Use the database address the platform gives you, usually in DATABASE_URL.

Can I connect to a Livops database from my laptop with its DATABASE_URL?

Not directly. Its host is an internal name such as postgres, reachable only from services in the same project, so your laptop cannot resolve it. Use Connect from your computer on the database page, which sets up an SSH tunnel, and connect to localhost through it.

Sources

  1. Node.js docs — Errors, common system errors (nodejs.org). Checked .
  2. Node.js docs — DNS, dns.lookup() (nodejs.org). Checked .
  3. Node.js docs — Net, socket.connect() options (nodejs.org). Checked .
  4. Node.js v26.14.0 source — lib/internal/errors.js (DNSException) (github.com). Checked .
  5. node-postgres — Connecting (node-postgres.com). Checked .
  6. Linux man-pages 6.19 — getaddrinfo(3) (man7.org). Checked .
  7. Linux man-pages — resolv.conf(5) (man7.org). Checked .
  8. Docker docs — Networking in Compose (docs.docker.com). Checked .
  9. MDN — String, string coercion (developer.mozilla.org). Checked .
  10. Railway docs — Private networking (docs.railway.com). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your Node.js app

Got a question? — @livops_uz