TypeError: fetch failed (ECONNREFUSED)
TypeError: fetch failed
[cause]: Error: connect ECONNREFUSED 127.0.0.1:3000In one sentence: Your server-side code tried to call an address where nothing was listening, very often localhost.
What it means
ECONNREFUSED means the computer at that address answered "nothing is running on that port." `127.0.0.1` and `localhost` always mean "this same machine," so on a server they point at the server itself, not your laptop.
In Next.js this usually happens when a Server Component or API route fetches your own API with a full URL like `http://localhost:3000/api/...`. It may work on your computer and then fail once deployed, or during the build when no server is running yet.
Common causes
- Fetching `http://localhost:3000/...` from server code in production, where your app isn't on localhost.
- A Server Component fetching its own API route during `next build`, before any server exists.
- The backend, database, or local API you are calling isn't running, or is on a different port.
- An environment variable for the API URL is missing in production, so the code falls back to localhost.
How to fix it
- In Server Components, don't fetch your own API routes. Call the shared function or database query directly instead.
- When you must call another service, read its URL from an environment variable and set that variable in your host's settings for production.
- Locally, make sure the service you're calling is actually running on that port.
- Print the full URL right before the fetch so you can see what address it's really using.
Before
// app/page.tsx (Server Component)
const res = await fetch("http://localhost:3000/api/products");
const products = await res.json();After
// app/page.tsx (Server Component)
import { getProducts } from "@/lib/products"; // same code the API route uses
const products = await getProducts();Tried this and it's still broken? Paste your exact error for a free explanation of your specific case.
Prompt to paste into your AI tool
My Next.js server code fails with 'fetch failed ECONNREFUSED 127.0.0.1'. Replace server-side fetches to my own API routes with direct calls to the shared data functions, move any external URLs into environment variables, and don't hardcode localhost.Still stuck?
Paste your exact error and code. Get a plain-English explanation free, or the full fix for $5.
Hitting errors every day? Builder is $15/mo for 100 fixes per month. Compare plans
Related errors
- Error: Invalid hook call. Hooks can only be called inside of the body of a function component
- npm ERR! ERESOLVE unable to resolve dependency tree
- Error: listen EADDRINUSE: address already in use :::3000
- Stripe: No signatures found matching the expected signature for payload
Weekly: the errors breaking vibe-coded apps and how to fix them. Free.
No spam. Unsubscribe anytime.