Skip to main content

Node.js · 5 common causes

ECONNREFUSED

Updated

The connection reached the host and was actively refused — nothing is listening on that port.

The exact message

Error: connect ECONNREFUSED 127.0.0.1:5432
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 5432
}

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is ECONNREFUSED.

How to fix it

Cause 1 of 5

The service is not running.

Start it, then verify something is actually listening: lsof -i :5432 or ss -ltn | grep 5432. If it exits straight after starting, its own log holds the reason — read that rather than retrying the client.

Cause 2 of 5

Inside Docker Compose, localhost means the container itself.

Each container has its own loopback interface. Use the compose service name as the hostname — postgres:5432, not 127.0.0.1:5432 — since services resolve each other by name on the shared network.

Cause 3 of 5

The server is bound to an interface you are not reaching.

A process bound to 127.0.0.1 refuses connections from other machines and other containers. Bind 0.0.0.0 to accept them — and if you do that, make sure the port is not also exposed to the public internet by your firewall or security group.

Cause 4 of 5

The client connects before the dependency is ready.

Compose's depends_on waits for the container to start, not for the database to accept connections. Add a healthcheck and condition: service_healthy, or retry the connection with a short exponential backoff at startup.

Cause 5 of 5

localhost resolved to IPv6 while the server bound IPv4.

Some resolvers answer localhost as ::1 first. Put 127.0.0.1 in the connection string explicitly, or configure the server to listen on both stacks.

Stopping it coming back

Use service names rather than localhost in every containerised config, and give each dependency a healthcheck so the app waits for readiness instead of racing it.

ECONNREFUSED — common questions

What is the difference between ECONNREFUSED and ETIMEDOUT?
ECONNREFUSED means a machine answered and rejected the connection — the route works, the listener does not exist. ETIMEDOUT means nothing answered at all, which usually points at a firewall, a security group, or the wrong host entirely.
Why does it work on my machine but not in Docker?
Because localhost is a different thing in each place. On your machine it is the host, where the database is running; in a container it is that container, where nothing is listening. Swapping in the service name fixes it without changing anything else.
How do I check quickly whether a port is reachable?
nc -zv host 5432 or curl -v telnet://host:5432 gives an immediate answer, and separates 'refused' from 'no route' clearly. Run it from the same place your app runs — from inside the container, not from the host.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor