Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

React hides an error and only says a cross-origin error was thrown

ErrorUncaught Error: A cross-origin error was thrown. React doesn't have access to the actual error object in development.

Short answer

The browser hides the details of an error that comes from a script it counts as cross-origin, so React cannot log the original. The usual causes are a CDN script without the crossorigin attribute or CORS header, a bundler that wraps development code in eval, and code-split bundles loaded without crossorigin.

  • Applies to React 18 and 19, webpack 5, current browsers
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List the script tags of the page and see which ones load from another origin, such as a CDN. Those are the files whose errors are hidden.

    curl -s http://localhost:3000 | grep -o '<script[^>]*>'
  2. 2Match what you see, then jump to its fix

    1. 1The page loads React or another library from a different host with a plain script tag.A CDN script has no crossorigin attributeFix
    2. 2The page loads only your own bundle, yet the error is still hidden.The bundler wraps development code in evalFix
    3. 3The error appears only after navigating to a route that loads an extra chunk.Code-split bundles load without crossoriginFix

What does the error look like?

  • Uncaught Error: A cross-origin error was thrown. React doesn't have access to the actual error object in development.

Cause 1 of 3

A CDN script has no crossorigin attribute

1Check

React's documentation says to add the crossorigin attribute to scripts from another origin, and the CDN must send an Access-Control-Allow-Origin header. MDN says that without CORS, error logging through window.onerror is limited.

curl -sI https://cdn.example.com/lib.js | grep -i access-control-allow-origin

2Fix

Add crossorigin to the tag. If the CDN does not send the header, host the file yourself or use a CDN that does.

<script crossorigin src="https://cdn.example.com/lib.js"></script>

3Verify

Reload. The console now shows the real error message and stack.

# reload the page and read the console

Confirmed by legacy.reactjs.org

Did this fix it?

Cause 2 of 3

The bundler wraps development code in eval

1Check

React's documentation notes that webpack devtool settings that use eval make errors look cross-origin. Look at the devtool value.

grep -rn devtool webpack.config.js

2Fix

Use a source map setting that does not use eval for development.

// webpack.config.js
module.exports = {
  devtool: 'cheap-module-source-map',
};

3Verify

Restart the dev server. The error shows its real message.

npm start

Confirmed by legacy.reactjs.org

Did this fix it?

Cause 3 of 3

Code-split bundles load without crossorigin

1Check

For the web target webpack loads on-demand chunks by adding script tags itself, so the attribute comes from its output.crossOriginLoading setting, which is off by default. The webpack docs list the values false, anonymous and use-credentials.

grep -rn crossOriginLoading webpack.config.js

2Fix

Set crossOriginLoading in the development config.

// webpack.config.js
module.exports = {
  output: {
    crossOriginLoading: 'anonymous',
  },
};

3Verify

Open the Elements panel and check the generated chunk script tags carry crossorigin.

# inspect the chunk script tags in the browser

Confirmed by webpack.js.org

Did this fix it?

How do I stop it coming back?

  • Load libraries from your own build instead of third-party hosts when you can.
  • Add crossorigin to every third-party script tag, and test that the host sends the CORS header.
  • Use a non-eval source map setting for development.

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

Next step

Want this handled for good?

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