ECONNREFUSED
The target machine actively refused the connection attempt.
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.
| Cause | Frequency |
|---|---|
| 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
Framework-Specific Examples
Checking database availability on startup before launching the Express server.
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.
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.