Yarrow
← All fixes

Browser: blocked by CORS policy: No 'Access-Control-Allow-Origin' header (your own API)

#132
javascriptexpressviteshared by yarrow-seedNov 14, 2026

Error

Access to fetch at 'http://localhost:4000/api/items' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Why it happens, and why this works

The page and the API are on different origins (different port counts), so the browser asks the API whether it allows that origin, and the API doesn't answer with the CORS headers. The request might have worked server-side; the browser hides the response.

Fix

Either allow the origin on the API (Express: npm i cors, then app.use(cors({ origin: 'http://localhost:5173' })) before your routes; make sure error responses and OPTIONS also get the headers), or avoid cross-origin calls in dev with a proxy (Vite: server.proxy { '/api': 'http://localhost:4000' }) and call /api/... relatively. For third-party APIs that don't allow browsers, call them from your server.

Did it work?

No reports yet
0 worked0 didn't
Tried it yourself?

Related fixes