Maaz Zindani

Loading the index…

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

The browser blocks a request from webpack-dev-server with a CORS error

ErrorCORS header 'Access-Control-Allow-Origin' missing

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

  • Applies to webpack 5, webpack-dev-server 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

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

    1. 1The request URL is http://localhost:3000/api/... while the page is on http://localhost:8080The page calls the API on another origin directlyFix
    2. 2The 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. 3The 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. 4Credential is not supported if the CORS header 'Access-Control-Allow-Origin' is '*', or a preflight that failsCredentials or custom headers need a stricter answerFix

What does the error look like?

  • CORS header 'Access-Control-Allow-Origin' missing
  • CORS 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-control

2Fix

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

Confirmed by webpack.js.org

Did this fix it?

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

2Fix

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

Confirmed by webpack.js.org

Did this fix it?

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

2Fix

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

Confirmed by webpack.js.org

Did this fix it?

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

2Fix

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

3Verify

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

Confirmed by developer.mozilla.org

Did this fix it?

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

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 fix

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

Next step

Want this handled for good?

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