Node.js

ECONNREFUSED ::1 after a Node upgrade: localhost has two addresses

The database is running, psql connects fine, and Node insists the connection was refused. Since Node 17, localhost can mean the IPv6 address first, and many local services only listen on IPv4. Here is how to confirm it and the three ways to fix it.

You upgrade Node, start the app, and it cannot reach the local database: Error: connect ECONNREFUSED ::1:5432. Postgres is running. psql connects without complaint. Nothing changed except the Node version, and that is exactly the clue.

What does ECONNREFUSED ::1 mean?

ECONNREFUSED means the machine at that address answered and refused the connection, because nothing was listening on that port. The interesting part is the address. ::1 is the IPv6 version of localhost. The IPv4 version is 127.0.0.1. Most systems map the name localhost to both.

So the error reads: Node looked up localhost, picked ::1, and found nothing listening there. Meanwhile the database is happily listening on 127.0.0.1, which is the address psql or your browser reached.

Why did it start after a Node upgrade?

Because Node 17 changed how it orders the addresses a name resolves to. Up to Node 16, dns.lookup() reordered the results to put IPv4 first. From Node 17, it returns them in the order the operating system gives, which on many systems puts ::1 first. Code that never mentioned IPv6 started connecting over it.

Diagram: localhost has two addresses, the IPv6 address ::1 and the IPv4 address 127.0.0.1. Up to Node 16, dns.lookup reordered results to try 127.0.0.1 first. From Node 17, results come back in the order the operating system gives, often ::1 first. A database that only listens on 127.0.0.1 refuses the connection to ::1, so the app fails with ECONNREFUSED ::1:5432. Node 20 tries the other address automatically for most connections, but older versions and libraries that resolve the name themselves still fail. The fixes: connect to 127.0.0.1, make the service listen on ::1 too, or set the DNS result order to ipv4first.
Same name, two addresses, and the service is only listening on one of them.

Node 20 softened this. Its net.connect() now tries the other address family automatically when the first one fails, an approach called Happy Eyeballs, and it is on by default. You still meet the error on Node 18, and in drivers or libraries that resolve the name themselves and then connect to a single address, which bypasses that fallback.

How do you confirm it in a minute?

Two commands settle it. First, see the order Node gets for localhost:

node -e "require('dns').lookup('localhost', { all: true }, (e, a) => console.log(a))"

If ::1 comes first, check which addresses the service actually listens on. On Linux:

ss -ltn 'sport = :5432'

127.0.0.1:5432 alone means IPv4 only, which confirms the diagnosis. [::1]:5432 or *:5432 means the service listens on IPv6 too, and the cause lies elsewhere, often a service that is simply not running or a port typo.

Which fix should you use?

1. Connect to 127.0.0.1 instead of localhost. The simplest and most explicit fix. Change the host in your configuration, for example DB_HOST=127.0.0.1, and there is no name to resolve and nothing to reorder.

2. Make the service listen on both addresses. Most databases and caches can listen on ::1 as well as 127.0.0.1, through their bind or listen address setting. This also fixes every other client on the machine that prefers IPv6.

3. Ask Node for the old order. When you cannot change the address, for example in a third-party tool, tell Node to put IPv4 first again, either from the command line or at the top of your entry point:

NODE_OPTIONS=--dns-result-order=ipv4first node server.js

// or in code, before anything connects
require('dns').setDefaultResultOrder('ipv4first');

If the process will not start at all because something already owns its port, that is a different error with a different fix: EADDRINUSE: address already in use.

Found something wrong in here? Tell me.

Checking API...
Checking AI Gateway...
Check status
⚠️ Setup Required