Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

fetch() throws TypeError: Failed to fetch in the browser

ErrorTypeError: Failed to fetch

Short 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.

  • Applies to browser fetch API, Express 5, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/items
  2. 2Match what you see, then jump to its fix

    1. 1curl 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. 2The page is on https:// and the fetch URL starts with http://.An HTTP API is called from an HTTPS page (mixed content)Fix
    3. 3Works 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. 4The error appears at once, and no request shows in the Network tab.The URL or an option is invalidFix

What does the error look like?

  • TypeError: Failed to fetch
  • Uncaught (in promise) TypeError: Failed to fetch
  • Cross-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.js

2Fix

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/items

Confirmed by developer.mozilla.org

Did this fix it?

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/items

Confirmed by developer.mozilla.org

Did this fix it?

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 server

3Verify

curl -vI reports no certificate error, and the request works with extensions on.

curl -vI https://api.example.com/items

Confirmed by developer.mozilla.org

Did this fix it?

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 > Network

Confirmed by developer.mozilla.org

Did this fix it?

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:

Related fixes

Web 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 fix

Web 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 fix

Web 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.