The browser blocks a request from webpack-dev-server with a CORS error
CORS header 'Access-Control-Allow-Origin' missingShort answer
CORS errors in development mean the page (served by webpack-dev-server, for example on port 8080) asks another origin for data and that origin does not answer with the right headers. Easiest fix: let the dev server proxy the API path so the browser only talks to one origin. If the dev server's own files are the blocked ones, add headers there.
Fix it fast
1Find out which cause you have
Look at the failing request in the browser's Network tab, then compare the page origin with the request URL. If the scheme, host or port differs, it is a cross-origin request. Or check from the command line what the API answers for your dev origin.
curl -i -H "Origin: http://localhost:8080" http://localhost:3000/api/users2Match what you see, then jump to its fix
- 1
The request URL is http://localhost:3000/api/... while the page is on http://localhost:8080The page calls the API on another origin directlyFix - 2
The proxy is set up but the backend answers 404 or a different siteThe backend sits behind a virtual host that checks the Host headerFix - 3
The blocked URL is the dev server's own bundle, chunk, font or hot-update file, loaded from a page served elsewhereFiles from the dev server itself are loaded from another originFix - 4
Credential is not supported if the CORS header 'Access-Control-Allow-Origin' is '*', or a preflight that failsCredentials or custom headers need a stricter answerFix
- 1
What does the error look like?
CORS header 'Access-Control-Allow-Origin' missingCORS preflight channel did not succeed
Cause 1 of 4
The page calls the API on another origin directly
1Check
Any other port counts as another origin. The browser blocks the response unless the server answers with an Access-Control-Allow-Origin header.
curl -i -H "Origin: http://localhost:8080" http://localhost:3000/api/users | grep -i access-control2Fix
Route the call through the dev server. webpack-dev-server uses http-proxy-middleware, and a proxy entry forwards matching paths to the backend, so the browser sees only the dev server's own origin. Then change the client code to use the relative path /api/users.
// webpack.config.js
export default {
devServer: {
proxy: [
{
context: ["/api"],
target: "http://localhost:3000",
},
],
},
};3Verify
Restart the dev server and request the relative URL. The API data comes back with no CORS error.
curl -i http://localhost:8080/api/usersConfirmed by webpack.js.org
Cause 2 of 4
The backend sits behind a virtual host that checks the Host header
1Check
By default the dev server keeps the original host header when it proxies. A backend that serves several sites by name may reject it.
curl -i -H "Host: localhost:8080" http://api.example.test/api/users2Fix
Set changeOrigin to true on the proxy entry so the host header is replaced by the target's.
proxy: [
{
context: ["/api"],
target: "http://api.example.test",
changeOrigin: true,
},
],3Verify
Restart the dev server and call the relative path again.
curl -i http://localhost:8080/api/usersConfirmed by webpack.js.org
Cause 3 of 4
Files from the dev server itself are loaded from another origin
1Check
When another server's page pulls your bundle from the dev server, the browser needs CORS headers on the dev server's responses.
curl -I -H "Origin: http://localhost:4000" http://localhost:8080/main.js2Fix
Add the headers to every dev server response with devServer.headers, naming the one origin that may load it.
devServer: {
headers: {
"Access-Control-Allow-Origin": "http://localhost:4000",
},
},3Verify
Restart the dev server and repeat the curl: the header is in the response.
curl -I -H "Origin: http://localhost:4000" http://localhost:8080/main.js | grep -i access-controlConfirmed by webpack.js.org
Cause 4 of 4
Credentials or custom headers need a stricter answer
1Check
A request that sends cookies must get an explicit origin and Access-Control-Allow-Credentials: true. A request with custom headers or a non-simple method is preceded by an OPTIONS preflight that must be approved.
curl -i -X OPTIONS -H "Origin: http://localhost:8080" -H "Access-Control-Request-Method: POST" http://localhost:3000/api/users2Fix
Prefer the proxy from the first cause, which removes the preflight. If you must call the API directly, make the API answer with the exact origin, and with Allow-Methods and Allow-Headers lists, not a wildcard.
Access-Control-Allow-Origin: http://localhost:8080
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Headers: Content-Type3Verify
Repeat the preflight curl and read the response headers.
curl -i -X OPTIONS -H "Origin: http://localhost:8080" -H "Access-Control-Request-Method: POST" http://localhost:3000/api/usersConfirmed by developer.mozilla.org
How do I stop it coming back?
- Use relative API paths in client code and map them to a backend with a proxy in development.
- Keep one list of allowed origins on the real API, and never use a wildcard with cookies.
- Write the proxy entry once in the dev config, not in each component.
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
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
The cors-anywhere.herokuapp.com demo proxy stopped working
Missing required request header. Must specify one of: origin,x-requested-with
The public cors-anywhere.herokuapp.com server is only a demo, is rate limited, and since February 2021 requires an opt-in.
2 causes, each with a fixWeb apps
The browser rejects the CORS preflight because the OPTIONS reply is not a 2xx
The request in the Network tab is an OPTIONS call with a red status, and the real GET or POST never appears
Before some cross-origin calls the browser sends an OPTIONS preflight and treats any answer outside 200 to 299 as a network error.
3 causes, each with a fixWeb apps
A public website cannot reach your local dev server: local network request blocked
Look for and connect to any device on your local network
Chrome treats a request from a public website to localhost or a private IP as a local network request.
3 causes, each with a fixWeb apps
CORS fails when you open an HTML file straight from disk
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:///
A page opened from disk has an opaque origin, so browsers treat every request it makes as cross-origin, and CORS only works over http or https.
3 causes, each with a fixWeb apps
Firefox blocks a cross-origin request even though the CORS headers are set
Reason: Multiple CORS header 'Access-Control-Allow-Origin' not allowed
Firefox puts a Reason in the console message, and that text is the diagnosis.
5 causes, each with a fix