ECONNREFUSED in Node.js — What It Means and How to Fix It

What the Node.js/npm error ECONNREFUSED means and how to resolve it.

Published October 25, 2026

ECONNREFUSED means nothing is listening at the address and port you tried to connect to, so the connection was actively refused.

What it means and why it happens

Typical causes: the server (database, API) is not running, wrong host or port, or a container using localhost instead of the service name.

Read the full message: it names the file and line where the problem was detected, which is the best place to start.

What you see

run the code
Error: connect ECONNREFUSED 127.0.0.1:5432

Practical examples

Code that triggers it

await pool.connect()   // database is down

The fix

// start the service, then retry
const pool = new Pool({ host: process.env.DB_HOST, port: 5432 })

Check it is listening

nc -zv 127.0.0.1 5432
ss -ltnp | grep 5432

Common mistakes

  • Using localhost inside Docker.
  • Starting the app before the database.
  • Firewall rules.

Key takeaways

  • ECONNREFUSED: ECONNREFUSED means nothing is listening at the address and port you tried to connect to, so the connection was actively refused.
  • Read the message and the line number first.
  • Fix the cause, don't just silence the error.

FAQ

In Docker?

Use the service name (e.g. db) instead of localhost.

How do I find which line caused ECONNREFUSED?

The error message and stack trace show the file and line; start there and work backwards.

More JavaScript articles