---
title: "Error: connect ECONNREFUSED 127.0.0.1"
description: "ECONNREFUSED means nothing was listening on the address and port Node.js tried to reach. Learn how to find out why, including the "
url: https://www.danny.engineering/answer/nodejs/error-connect-econnrefused-127001
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T18:57:00.000Z
tags: ["Node.js"]
---

# Error: connect ECONNREFUSED 127.0.0.1

## 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.


```bash
# Is anything listening on the port? (macOS and Linux)
lsof -iTCP:5432 -sTCP:LISTEN

# Try the connection without Node.js
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.


```javascript
const port = Number(process.env.DB_PORT ?? 5432);
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.


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


### 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`.


```bash
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.

## Related reading

- [Node.js: Common system errors](https://nodejs.org/api/errors.html#common-system-errors)
- [Node.js: net.connect](https://nodejs.org/api/net.html#netconnect)
