fetch() throws TypeError: Failed to fetch in the browser
TypeError: Failed to fetchShort answer
Failed to fetch means the request never produced a response the browser would hand to your code. It is not an HTTP error: a 404 or 500 still resolves. Open the Network tab. The usual causes are a server that is down or at the wrong URL, an HTTP API called from an HTTPS page, or something blocking the request.
Fix it fast
1Find out which cause you have
Reproduce the call from a terminal. If curl also fails, the problem is the server or network. If curl works, the browser is blocking the request, so read the red row in DevTools > Network and the Console.
curl -i http://localhost:3000/api/items2Match what you see, then jump to its fix
- 1
curl fails with 'Connection refused' or a DNS error; the Network row is red with no status code.The server is not running, or the URL or port is wrongFix - 2
The page is on https:// and the fetch URL starts with http://.An HTTP API is called from an HTTPS page (mixed content)Fix - 3
Works in a private window with extensions off, or the URL is https:// with a self-signed or expired certificate.An invalid TLS certificate, or a browser extension, blocks the requestFix - 4
The error appears at once, and no request shows in the Network tab.The URL or an option is invalidFix
- 1
What does the error look like?
TypeError: Failed to fetchUncaught (in promise) TypeError: Failed to fetchCross-Origin Request Blocked: ... (Reason: CORS request did not succeed)
Cause 1 of 4
The server is not running, or the URL or port is wrong
1Check
The browser reports a refused connection, a DNS failure or a timeout as a network error, and fetch turns that into a TypeError. Check the Network tab for which one it is, and confirm that the process is listening.
curl -i http://localhost:3000/api/items
# in the Express app: confirm the same port is used
grep -n "listen" server.js2Fix
Start the API, and make the URL in the front end match its scheme, host and port.
const res = await fetch('http://localhost:3000/api/items');3Verify
curl returns a status line and the request in the browser shows a status code.
curl -I http://localhost:3000/api/itemsConfirmed by developer.mozilla.org
Cause 2 of 4
An HTTP API is called from an HTTPS page (mixed content)
1Check
A page served over HTTPS cannot freely load HTTP resources; the browser can fail the request at network level.
grep -rn "fetch('http://\|fetch(\"http://" src/2Fix
Serve the API over HTTPS and use that URL.
const res = await fetch('https://api.example.com/items');3Verify
Reload the page; the request now appears with a status code.
curl -I https://api.example.com/itemsConfirmed by developer.mozilla.org
Cause 3 of 4
An invalid TLS certificate, or a browser extension, blocks the request
1Check
Ad blockers and privacy tools can block a request, and so can an expired or invalid certificate.
curl -vI https://api.example.com/items 2>&1 | grep -i -E 'certificate|expire|verify'2Fix
Renew or correct the certificate on the server. For the extension case, disable it for your site, or try a private window to confirm.
# no code: renew the certificate on the server3Verify
curl -vI reports no certificate error, and the request works with extensions on.
curl -vI https://api.example.com/itemsConfirmed by developer.mozilla.org
Cause 4 of 4
The URL or an option is invalid
1Check
fetch() also rejects with TypeError when the URL is invalid, when the URL includes a username and password, or when an option has an invalid value.
console.log(new URL(url).href)2Fix
Build the URL with the URL class, remove credentials from it (send an Authorization header instead), and check that options such as method and headers have valid values.
const res = await fetch(new URL('/api/items', 'http://localhost:3000'), {
headers: { Authorization: `Bearer ${token}` },
});3Verify
A request row now appears in the Network tab.
# DevTools > NetworkConfirmed by developer.mozilla.org
How do I stop it coming back?
- Keep the API base URL in one config value per environment.
- Always check response.ok, because HTTP error statuses do not reject.
- Use HTTPS for the API in every environment that serves the page over HTTPS.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
Axios sends no cookie to an Express API even with withCredentials set to true
The login response has a Set-Cookie header, but the browser stores nothing
withCredentials only tells the browser to attach cookies.
4 causes, each with a fixWeb apps
Electron app blank with 'Refused to evaluate a string as JavaScript'
Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive
The page has a Content-Security-Policy without 'unsafe-eval', and some code calls eval or new Function.
3 causes, each with a fixWeb apps
response.json() fails after fetch with mode: no-cors
response.type is "opaque" and response.status is 0
no-cors does not bypass CORS; it hides the response.
4 causes, each with a fixWeb apps
Jest fails when a component imports an .svg file
A syntax error is reported for the .svg file, because Jest tries to parse it as JavaScript
Jest runs every imported file as JavaScript, and an .svg file is XML, not JavaScript.
3 causes, each with a fixWeb apps
A React component crashes while destructuring props or state
TypeError: Cannot destructure property 'b' of 'null' as it is null.
JavaScript throws this when the right side of a destructuring pattern is undefined or null.
3 causes, each with a fixWeb apps
Jest fails to parse a React Native or Expo dependency
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax
Jest does not run Babel on node_modules by default, and many React Native packages ship untranspiled source.
3 causes, each with a fix