Node.js ECONNREFUSED ::1:3000: localhost resolves to IPv6
Fix "connect ECONNREFUSED ::1:3000" in Node, fetch and tests: use 127.0.0.1 or make the server listen on both address types.
The name localhost can mean two addresses: 127.0.0.1 (IPv4) and ::1 (IPv6). Newer versions of Node try them in the order your system gives, so a client may try ::1 while your server only listens on 127.0.0.1, and the connection is refused.
What to do, step by step
- Read the address in the error. If you see ::1, the client tried the IPv6 address.
- Quick fix: call http://127.0.0.1:3000 instead of http://localhost:3000.
- Or make the server listen on both: leave out the host in listen, or use ::.
- Or set the order for the whole Node process: node --dns-result-order=ipv4first app.js.
- Check the server is really running, with curl http://127.0.0.1:3000.
Commands you can copy
node --dns-result-order=ipv4first app.jsexport NODE_OPTIONS=--dns-result-order=ipv4firstcurl http://127.0.0.1:3000Other causes to check
- "fetch failed" with no more detail
- Look at error.cause in the output. It holds the real ECONNREFUSED line and the address.
- Works in the browser but not in Node
- Browsers try both addresses, while Node used only the first one in older versions. Use 127.0.0.1 in your Node code and tests.
- Docker: connection refused to localhost
- Inside a container, localhost is the container itself. See the Docker page about reaching the host.
- The server is simply not running
- Start it first. A refused connection on both 127.0.0.1 and ::1 means nothing listens on that port.
Questions about Node ECONNREFUSED ::1
- What is ::1?
- It is the IPv6 version of 127.0.0.1. Both mean "this computer".
- Why did this start after updating Node?
- From Node 17 the name lookup keeps the order returned by the system instead of preferring IPv4, so ::1 can come first on some computers.
- Does this still happen on new Node versions?
- Newer Node versions try the next address when the first one fails, so it is seen mostly on older Node versions and some test tools.
- Should I always use 127.0.0.1?
- For local tests and scripts it is the simplest choice, because it avoids the lookup completely.