React hides an error and only says a cross-origin error was thrown
Uncaught 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.
Fix it fast
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[^>]*>'2Match what you see, then jump to its fix
- 1
The page loads React or another library from a different host with a plain script tag.A CDN script has no crossorigin attributeFix - 2
The page loads only your own bundle, yet the error is still hidden.The bundler wraps development code in evalFix - 3
The error appears only after navigating to a route that loads an extra chunk.Code-split bundles load without crossoriginFix
- 1
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-origin2Fix
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 consoleConfirmed by legacy.reactjs.org
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.js2Fix
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 startConfirmed by legacy.reactjs.org
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.js2Fix
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 browserConfirmed by webpack.js.org
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:
- 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.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Back end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
npm start stops with Could not find a required file in a React app
Name: index.html
react-scripts checks for two files before the dev server starts: public/index.html and a src/index file.
3 causes, each with a fixBack end
Create React App says Node is too old, and npm will not run on the new one
npm does not support Node.js v10.19.0
The shell is running an old Node.js that is below the version Create React App needs.
3 causes, each with a fixBack end
Flask-WTF rejects a React request even though the CSRF token is sent
400 Bad Request on POST, PUT, PATCH or DELETE from a React app
Flask-WTF checks two things: the token sent with the request and a copy kept in the session cookie.
4 causes, each with a fixBack end
jwtDecode throws InvalidTokenError in React Native
Property 'atob' doesn't exist
jwt-decode uses the global atob function.
2 causes, each with a fixBack end
CSRF token mismatch when a React app logs in to a Laravel Sanctum API
Access-Control-Allow-Credentials
With Sanctum's cookie-based SPA login, Laravel only accepts a state-changing request if the browser first got an XSRF-TOKEN cookie and sends it back with its session cookie.
4 causes, each with a fix