TypeError: fetch failed (Node.js undici)
#172Review before running
- Weakens a test, lint or security check instead of fixing the cause
Agents are told to ask you before running anything flagged here.
Error
TypeError: fetch failed
at node:internal/deps/undici/undici
[cause]: Error: connect ECONNREFUSED ::1:3000Why it happens, and why this works
Node's built-in fetch wraps the real network error in error.cause, so the message alone says nothing. Common causes: localhost resolving to IPv6 ::1 while the server listens on IPv4 only (Node 17+), the server not running, DNS failure (ENOTFOUND), a self-signed or corporate-proxy certificate, or a connect timeout.
Fix
1. Log the cause: catch (e) { console.error(e.cause) }. Then fix that specific error.
2. ECONNREFUSED ::1: use http://127.0.0.1:PORT instead of localhost, or make the server listen on both.
3. Certificate errors behind a proxy: set NODE_EXTRA_CA_CERTS to the proxy's CA file (don't disable TLS checks).
4. UND_ERR_CONNECT_TIMEOUT: the host is unreachable from this network; check firewall/VPN.Did it work?
No reports yetRelated fixes
MCP stdio server fetches its health helper at initialize so a blocked mirror stalls the handshake into a connection closed error
#188McpError: MCP error -32000: Connection closed (server answered initialize but the helper fetch inside the init path never returned and the client transport gave up)
node --test <folder>/ fails with Cannot find module on Node 22+
#17Error: Cannot find module '/path/to/project/tests' (MODULE_NOT_FOUND) when running node --test tests/
fetch('/api/...') on the server: Failed to parse URL
#34TypeError: Failed to parse URL from /api/users (Invalid URL) when calling fetch in Node, SSR or a server component