How to fix "Error: connect ECONNREFUSED"

Tested: we reproduced this error and confirmed the fix on Node 22.23.5 on Ubuntu 24.04, November 29, 2026.

ECONNREFUSED means “connection refused”: your code tried to connect to an address and port, and nothing there accepted the connection. The machine answered, but no program was listening on that port. Read the address in the error first:

Error: connect ECONNREFUSED 127.0.0.1:3999

Then check three things:

  1. Is the service running? Start your API, database or dev server and watch for a “listening on port” line.
  2. Is it on that port? The port in the error must match the one the service printed.
  3. Is it on that address? 127.0.0.1 (IPv4) and ::1 (IPv6) are different. A server listening on one doesn’t answer on the other.

We reproduced the error on Node 22.23.5 with nothing listening on the port. Both http.get to 127.0.0.1 and to localhost failed with connect ECONNREFUSED 127.0.0.1:3999, and fetch() failed with the less helpful fetch failed, with the real reason in error.cause.

When fetch() only says “fetch failed”

Node’s built-in fetch wraps connection errors. Log the cause to see it:

try {
  await fetch('http://localhost:3999/');
} catch (err) {
  console.log(err.cause);   // { code: 'ECONNREFUSED', ... }
}

Find your cause

Address in the error Likely cause Fix
127.0.0.1:PORT The service isn’t running, or it’s on another port Start it; match the port
::1:PORT localhost resolved to IPv6, server listens on IPv4 only Use 127.0.0.1 in your connection string
127.0.0.1:5432, :27017, :3306 Postgres, MongoDB or MySQL isn’t running locally Start the database, or point at the right host
127.0.0.1:PORT inside Docker localhost is the container Use the service name or host.docker.internal
A remote IP Firewall or the server is down Check the server and its firewall

Fix: the ::1 (IPv6) case

On machines with IPv6, localhost can resolve to ::1 first. If your database listens only on 127.0.0.1, connections to localhost are refused on ::1. The simple fix is to name the IPv4 address:

DATABASE_URL=postgres://user:pass@127.0.0.1:5432/app

Or make the server listen on both addresses. Our test machine had no IPv6, so localhost resolved only to 127.0.0.1 there; this case comes from Node’s and the databases’ documentation rather than our run.

Fix: check what’s listening

# macOS and Linux
lsof -iTCP -sTCP:LISTEN -n -P | grep 3000
# Linux
ss -tlnp | grep 3000
# Windows
netstat -ano | findstr :3000

If nothing is listed, the service isn’t running on that port. If something is listed on 127.0.0.1 but your error shows ::1 (or the other way round), use the address that’s listed.

Confirm it worked

The request returns a response instead of throwing. For a quick test outside your code, curl -v http://127.0.0.1:3000/ should show Connected to 127.0.0.1. If you now get Address already in use when starting the server, another process already holds the port.

FAQ

Why does ECONNREFUSED say ::1?

localhost resolved to the IPv6 address ::1, and the server only listens on IPv4 127.0.0.1. Connect to 127.0.0.1 directly, or make the server listen on both. This is common with databases such as Postgres and MongoDB on machines with IPv6.

Why does it work in the browser but not from Node?

They may resolve localhost differently, or the browser is using a proxy or a different port. Compare the exact address and port in the error with the one the browser uses.

What's the difference between ECONNREFUSED and ECONNRESET?

ECONNREFUSED: the connection was never accepted. ECONNRESET: it was accepted and then dropped, often by a server crash, a timeout or a proxy closing it.

How do I fix ECONNREFUSED in Docker?

Inside a container, localhost is the container itself. Use the other service's name from docker-compose (for example db:5432), or host.docker.internal to reach your machine.