Node.jsRunbook 0/4 done
ECONNREFUSED

Node.js Error ECONNREFUSED: Connection refused

Error↻ Retryable · exponential backoffNetwork
TL;DR · 10-second read

The target host answered with a TCP RST: nothing is listening on that address and port. Start the service, fix the host/port, and on Node 17+ use 127.0.0.1 instead of localhost if the server only binds IPv4.

Identify: does your log match?

What you see · exact log / response
Error: connect ECONNREFUSED ::1:5432
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1607:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 5432
}
  1. High

    localhost resolves to IPv6 ::1

    Since Node 17, DNS results keep OS order, so localhost often becomes ::1. A server bound only to 127.0.0.1 refuses the IPv6 connection.

  2. High

    Service not running or wrong port

    The database or API has not started yet, crashed, or listens on a different port than configured.

  3. Medium

    localhost inside a container

    In Docker, localhost is the container itself; the database runs in another container reachable by service name.

Fix

Connect to an explicit IPv4 address or prefer IPv4

✕ Broken · Node.js
const client = new pg.Client({ host: 'localhost', port: 5432 });
✓ Fixed · Node.js
import dns from 'node:dns';
dns.setDefaultResultOrder('ipv4first'); // or run node --dns-result-order=ipv4first

const client = new pg.Client({ host: process.env.PGHOST ?? '127.0.0.1', port: 5432 });

Using 127.0.0.1, or preferring IPv4 results, matches servers that only listen on IPv4.

Verify: reproduce it on purpose

Reproduce locally · node
$ node -e "require('net').connect(9, '127.0.0.1').on('error', e => console.log(e.code))"

Run it against a dev environment to trigger ECONNREFUSED on purpose and test your error handler.

Prevent

  • Wait for dependencies with healthchecks, not sleep.
  • Read host/port from env, never hardcode localhost.
  • Retry startup connections with backoff.

FAQ

Why did ECONNREFUSED start after upgrading to Node 18?

Node 17 stopped reordering DNS results to IPv4 first. localhost now resolves to ::1 on many systems; use 127.0.0.1 or --dns-result-order=ipv4first.

ECONNREFUSED vs ETIMEDOUT?

ECONNREFUSED means the host actively rejected the connection (nothing listening). ETIMEDOUT means no reply at all, usually a firewall or wrong IP.

What does errno -111 vs -61 mean?

Both are ECONNREFUSED: -111 on Linux, -61 on macOS. The numeric errno is platform-specific; use err.code.

Related errors

Last verified 2026-10-22 · Source: POSIX errno + Node.js ERR_* codes (doc/api/errors.md) · Official docs ↗ · ~33,100 searches/mo