TypeError: Failed to fetch / NetworkError when attempting to fetch resource

fetch() gives up before it ever gets a response, and the browser deliberately hides the reason from your JavaScript. The real cause is almost always printed one line above in the console, or visible in the Network tab.

Same error, three browsers

BrowserMessage
Chrome / EdgeTypeError: Failed to fetch
FirefoxTypeError: NetworkError when attempting to fetch resource.
SafariTypeError: Load failed

For security, browsers give scripts the same vague error whatever went wrong, so a page cannot probe your network. DevTools, which only you can see, shows the details.

Important: HTTP errors do not throw

const res = await fetch('/api/thing');   // a 404 or 500 does NOT throw
if (!res.ok) throw new Error('HTTP ' + res.status);
const data = await res.json();

If you are seeing Failed to fetch, the problem is earlier than a status code.

Find the cause in two minutes

  1. Console: look for a red line near the error. It usually says one of the causes below in plain words.
  2. Network tab: find the request. The Status column shows (blocked:mixed-content), (blocked:csp), (blocked:other), CORS error, (failed) net::ERR_... or (canceled).
  3. Open the URL directly in a new tab, or with curl -i URL. If that fails too, it is the server, DNS or network. If it works, it is a browser policy.

The usual causes

What you seeCauseGuide
“blocked by CORS policy”Server did not send Access-Control-Allow-Origin for your origin, or the preflight failedCORS errors, preflight, credentials
“Mixed Content”HTTPS page calling an http:// APIMixed content
“violates ... connect-src”Your own Content Security Policy blocks that hostconnect-src
ERR_BLOCKED_BY_CLIENTAd blocker or privacy extension (URLs with “analytics”, “track”, “ads”)ERR_BLOCKED_BY_CLIENT
ERR_NAME_NOT_RESOLVEDTypo in the host, or DNS not set upDNS errors
ERR_CONNECTION_REFUSEDNothing listening on that host/port; dev server not running, wrong port, or localhost vs 127.0.0.1 / IPv6localhost refused, ECONNREFUSED ::1
ERR_CERT_...Self-signed, expired or wrong-name certificate on the APIexpired cert, wrong name
(canceled)Page navigated away or a form submit reloaded the page mid-request; an AbortController firedCall event.preventDefault() in the submit handler

Quick fixes that are often it

Related

TypeError: fetch failed (Node / undici) · net::ERR_CONNECTION_TIMED_OUT · HTTP 504 Gateway Timeout · CORS errors explained · Mixed content blocked · ERR_BLOCKED_BY_CLIENT · Refused to connect (connect-src) · React hydration failed · ETIMEDOUT / connect ETIMEDOUT · ECONNRESET · getaddrinfo ENOTFOUND · WebRTC ICE failed · WebSocket connection failed · HTTP 206 Partial Content

Questions

What does TypeError: Failed to fetch mean?

The browser could not get any HTTP response for the request: it was blocked by CORS, mixed content, CSP or an extension, or the connection failed because of DNS, a wrong port, a certificate or being offline.

Does a 404 or 500 cause Failed to fetch?

No. fetch resolves normally for HTTP error statuses; check response.ok yourself. Failed to fetch means no usable response arrived at all.

Why is the error message so vague?

Browsers hide network details from scripts for security. The real reason is shown in the DevTools console and Network tab.

Is NetworkError when attempting to fetch resource the same error?

Yes. That is Firefox's wording; Safari says Load failed. The causes and fixes are the same.

How do I fix Failed to fetch caused by CORS?

The server must send Access-Control-Allow-Origin for your page's origin and answer the OPTIONS preflight. In development, a same-origin proxy avoids CORS entirely.

Published .

Try it

Related guides

Newest guides