TypeError: Failed to fetch
TypeError: Failed to fetchIn one sentence: The browser couldn't complete the request at all: the server is down, the URL is wrong, CORS blocked it, or you're calling http from an https page.
What it means
This error comes from the browser before any response arrives, so there's no status code. Something stopped the request from reaching the server or stopped the browser from reading the reply.
Open the browser's Network tab: the failed request there, and any red message in the Console next to it, tells you which of the causes below it is.
Common causes
- The backend isn't running, or the URL points to `localhost` after you deployed.
- A CORS error: the server didn't send `Access-Control-Allow-Origin` for your site.
- Mixed content: your https site calls an `http://` API.
- A typo in the URL or a missing environment variable, so the request goes to `undefined/api/...`.
- An ad blocker or browser extension blocked the request.
How to fix it
- Copy the request URL from the Network tab and open it directly. If it doesn't load, fix the server or the URL first.
- Replace hard-coded `localhost` URLs with an environment variable set for production, or use a relative path like `/api/...` for your own routes.
- If the Console shows a CORS message, allow your site's origin on the server (see the CORS guide).
- Make sure every API URL your https site calls also uses https.
- Try once in a private window with extensions off to rule out a blocker.
Before
const res = await fetch("http://localhost:3001/api/items");After
const base = process.env.NEXT_PUBLIC_API_URL ?? "";
const res = await fetch(`${base}/api/items`);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 frontend throws 'TypeError: Failed to fetch'. Find every fetch call, check for hard-coded localhost or http:// URLs and missing environment variables, switch them to an env-based https URL or a relative /api path, and tell me if the server needs CORS headers. Don't change unrelated code.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
- Supabase error: JWT expired
- Error: Dynamic server usage: Route couldn't be rendered statically because it used cookies
- Module not found: Can't resolve 'fs'
- Next.js: params should be awaited before using its properties
Weekly: the errors breaking vibe-coded apps and how to fix them. Free.
No spam. Unsubscribe anytime.