More Node.js answers

by Danny
Published 2 min read

ECONNREFUSED means nothing was listening on the address and port Node.js tried to reach. Learn how to find out why, including the

The short answer

#

Node.js reached the machine but nothing was accepting connections on that port, so the operating system refused the connection. Usually the server you are calling (a database, an API, a dev server) is not running, is on a different port, or is listening on a different address. Start it, then check the port it reports matches the one in your connection string.


_10
# Is anything listening on the port? (macOS and Linux)
_10
lsof -iTCP:5432 -sTCP:LISTEN
_10
_10
# Try the connection without Node.js
_10
nc -vz 127.0.0.1 5432

Why it happens

#

A full message looks like Error: connect ECONNREFUSED 127.0.0.1:5432. The part after the colon is the port Node.js tried. ECONNREFUSED is an operating system error, not a Node.js one: the TCP connection reached the host and was actively rejected because no process had that port open.

That is different from a timeout (ETIMEDOUT), where packets go nowhere, and from ENOTFOUND, where the hostname could not be resolved. Refused means the address was fine and the door was shut.

The common causes are:

  • The service is not running, crashed on start, or is still starting up.
  • The port in your config does not match the port the service uses.
  • The service listens on IPv6 ::1 while you connect to IPv4 127.0.0.1, or the other way round.
  • Your code runs inside a Docker container, where 127.0.0.1 means the container itself, not your laptop.

How to fix it

#

Check the service is running on the port you expect

Start the service and read its startup log. Most print the address and port they bound to. Then compare that with your environment variables or connection string. A missing variable often falls back to a default port in your code, so log the final value you connect with.


_10
const port = Number(process.env.DB_PORT ?? 5432);
_10
console.log(`Connecting to ${process.env.DB_HOST}:${port}`);

Wait for the service in scripts and CI

If you start the server and the client at the same moment, the client can win the race. Use your tool's health check (for example depends_on with condition: service_healthy in Docker Compose), or retry with a short delay before failing.


_10
async function connectWithRetry(connect, attempts = 5) {
_10
for (let i = 1; i <= attempts; i++) {
_10
try {
_10
return await connect();
_10
} catch (error) {
_10
if (error.code !== 'ECONNREFUSED' || i === attempts) throw error;
_10
await new Promise((resolve) => setTimeout(resolve, 1000 * i));
_10
}
_10
}
_10
}

Match IPv4 and IPv6

localhost can resolve to ::1 (IPv6) or 127.0.0.1 (IPv4). If the error shows ::1 but your server only listens on IPv4, or the reverse, connect to the explicit address the server uses, or make the server listen on both. Being explicit in config (127.0.0.1 rather than localhost) removes the guesswork.

Use the right host inside Docker

Inside a container, 127.0.0.1 points at that container. To reach another Compose service, use its service name as the host, such as db. To reach something running on your host machine, Docker Desktop provides host.docker.internal. On Linux you may need to add it with --add-host=host.docker.internal:host-gateway.


_10
DATABASE_URL=postgres://app:secret@db:5432/app

Also check the service inside the other container listens on 0.0.0.0, not only on its own loopback address, or other containers cannot reach it.

Common pitfalls

#
  • Restarting your Node.js app over and over. The problem is almost always the other side of the connection.
  • Reading the wrong port. The number in the error is what your code used, which may not be what you configured.
  • Forgetting that a firewall usually causes a timeout rather than a refusal, so ECONNREFUSED points away from firewall rules.
  • Leaving a production config pointing at localhost after moving the database to its own server.
  • Swallowing the error in a retry loop forever. Retry a few times, then fail with a clear message.
#

Was this resource helpful?