ECONNREFUSED

Node.js ErrorNetwork ErrorCommonLast updated: June 28, 2026Tested on:Node.js v20 LTSNPM v10.2June 2026

The target machine actively refused the connection attempt.

ECONNREFUSED Quick Fix⏱️ Est. Fix Time: 4 minutes

Usually happens because:

  • Target Service Offline
  • Wrong Port Configuration
  • Docker Localhost Isolation

🔍 Quick Checklist:

What is ECONNREFUSED?

ECONNREFUSED stands for 'Connection Refused'. It indicates that Node.js attempted to establish a TCP connection to a specific port and IP address, but the target machine actively rejected the request. This occurs when there is no application listening on that port, or when system-level firewalls and network routing rules block socket requests.

Common Causes

  • Target Service Offline: The database, cache, or external microservice is not running.
  • Wrong Port Configuration: The code is querying the wrong port (e.g., hitting port 3000 instead of 8080).
  • Docker Localhost Isolation: Querying 'localhost' inside a Docker container to hit a database running on the host machine.
  • Firewall Blocking: Security groups, firewalls, or routing tables are dropping/refusing connection packages.
CauseFrequency
Target Service Offline⭐⭐⭐⭐⭐
Wrong Port Configuration⭐⭐⭐⭐
Docker Localhost Isolation⭐⭐⭐

Common Mistakes

  • Forgetting to start local datastores (Redis, Postgres, Mongo) before initiating development servers.
  • Using localhost inside Docker containers instead of utilizing Docker networks.

How to Fix

1Verify Service Status: Ensure the target service (Redis, PostgreSQL, MongoDB, etc.) is online and running.
2Double-Check Ports and Hostnames: Verify connection parameters in environment variables.
3Configure Docker Networks: Use container service names or host.docker.internal for container-to-host queries.
4Inspect Firewall Rules: Ensure that the target port is open for incoming connections from your app server.

Framework-Specific Examples

Checking database availability on startup before launching the Express server.

Express Startup DB Check Example
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/myapp')
  .then(() => {
    app.listen(3000, () => console.log('App running on port 3000'));
  })
  .catch(err => {
    if (err.code === 'ECONNREFUSED') {
      console.error('MongoDB is offline. Exiting process.');
      process.exit(1);
    }
  });

Server Configuration Examples

Defining multiple backup servers in Nginx to prevent complete service downtime.

Nginx Upstream Configurations Config
upstream app_servers {
    server 127.0.0.1:3000;
    server 127.0.0.1:3001 backup;
}

Best Practices

  • Use environment variables for hostnames and ports.
  • Implement server dependency health-check loops in deployment configurations.

Frequently Asked Questions (FAQ)

Q: Why does localhost throw ECONNREFUSED in Node but works in the browser?

Browser clients fall back between IPv4 (127.0.0.1) and IPv6 (::1) address lookups automatically. Node.js resolves localhost based on OS hosts priority configuration. Try explicitly querying `127.0.0.1` in Node.

Q: What is the difference between ECONNREFUSED and ETIMEDOUT?

ECONNREFUSED indicates the target server was reached instantly but explicitly rejected the connection. ETIMEDOUT means the request got no response whatsoever, eventually exceeding timeout limits.

Q: How do I fix ECONNREFUSED for Docker containers?

Do not use 'localhost' to query other containers. Put all containers on the same Docker network and use the target container's service name as the host name.

Q: Can firewalls cause ECONNREFUSED?

Yes. If local firewall rules (like iptables, ufw, or Windows Firewall) block incoming traffic on a port, the OS returns a connection refused packet.

Still having this problem?

Didn't solve your problem?

SuggestRequest Error