Vite CORS Error: Access to Fetch Blocked— Fixed [Complete Guide 2026]
Also covers: No Access-Control-Allow-Origin · credentials · proxy setup · production CORS
⚡ The Error
Access to fetch at 'http://localhost:3000/api/users'
from origin 'http://localhost:5173' has been blocked
by CORS policy: No 'Access-Control-Allow-Origin'
header is present on the requested resource.✅ Fastest Fix — Vite Proxy
export default defineConfig({
server: {
proxy: {
"/api": "http://localhost:3000", // browser never sees the cross-origin request
},
},
})Why CORS Errors Happen in Vite Projects
CORS (Cross-Origin Resource Sharing) is a browser security policy, not a Vite limitation. When your Vite app at localhost:5173 fetches data from a backend at localhost:3000 — even on the same machine — the browser treats them as different origins and blocks the request unless the backend explicitly allows it.
The two ways to fix this are: (1) make the request appear same-origin using Vite's dev server proxy, or (2) configure your backend to send the correct Access-Control-Allow-Origin response headers. The proxy approach is simpler for development and requires no backend changes.
Use Vite's Dev Server Proxy
Best fix for development — no backend changes neededVite's server.proxy forwards matching requests from the Vite dev server — not the browser — to your backend. Since the request originates from a server (not a browser), CORS restrictions don't apply.
import { defineConfig } from "vite"
import react from "@vitejs/plugin-react"
export default defineConfig({
plugins: [react()],
server: {
proxy: {
// Requests to /api/* are forwarded to localhost:3000/api/*
"/api": {
target: "http://localhost:3000",
changeOrigin: true,
// Optional: rewrite the path prefix
// rewrite: (path) => path.replace(/^/api/, ""),
},
},
},
})// Before (cross-origin — triggers CORS error)
fetch("http://localhost:3000/api/users")
// After (relative path — goes through Vite proxy)
fetch("/api/users")
// With multiple backends:
// proxy: { "/api": "http://localhost:3000", "/auth": "http://localhost:4000" }After changing vite.config.ts, restart the dev server for the proxy to take effect. Hot Module Replacement does not reload vite.config.ts changes.
Add CORS Headers to Your Backend
Required for production — fix on the server sideFor production (where the Vite proxy doesn't exist), your backend must send the correct CORS headers. Here's how to configure CORS in the most common Node.js frameworks:
npm install cors
npm install --save-dev @types/corsimport express from "express"
import cors from "cors"
const app = express()
app.use(cors({
origin: [
"http://localhost:5173", // Vite dev
"https://yourapp.vercel.app", // Production frontend
],
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allowedHeaders: ["Content-Type", "Authorization"],
credentials: true, // needed for cookies
}))
app.get("/api/users", (req, res) => {
res.json({ users: [] })
})
app.listen(3000)import Fastify from "fastify"
import cors from "@fastify/cors"
const app = Fastify()
await app.register(cors, {
origin: ["http://localhost:5173", "https://yourapp.com"],
credentials: true,
})Fix CORS With Credentials (Cookies / Auth Headers)
CORS works but fails with credentials: includeIf you use credentials: 'include' in your fetch calls (needed for cookies and session-based auth), both the client and server need additional configuration. Using a wildcard * for the origin is not allowed when credentials are enabled.
// Must use credentials: "include" for cookies to be sent
fetch("/api/profile", {
credentials: "include",
headers: {
"Content-Type": "application/json",
},
})// vite.config.ts
server: {
proxy: {
"/api": {
target: "http://localhost:3000",
changeOrigin: true,
secure: false,
configure: (proxy) => {
proxy.on("proxyReq", (proxyReq) => {
// Forward cookies through the proxy
proxyReq.setHeader("origin", "http://localhost:3000")
})
},
},
},
}app.use(cors({
// ❌ Wrong — wildcard forbidden with credentials
// origin: "*",
// ✅ Correct — explicit origin required
origin: "http://localhost:5173",
credentials: true, // Access-Control-Allow-Credentials: true
}))Never use origin: '*' with credentials: true. The browser will block the request — this is not a bug in Vite or your backend. Always specify an explicit origin when using credentials.
Fix CORS in Production
Vite proxy only works in developmentThe Vite dev server proxy does not run in production (npm run build produces static files). In production, use a reverse proxy to route API calls through the same origin as your frontend, or configure your backend with the correct CORS headers for your production domain.
{
"rewrites": [
{
"source": "/api/:path*",
"destination": "https://your-backend.com/api/:path*"
}
]
}[[redirects]]
from = "/api/*"
to = "https://your-backend.com/api/:splat"
status = 200
force = trueserver {
listen 80;
server_name yourapp.com;
# Serve Vite static files
location / {
root /var/www/dist;
try_files $uri $uri/ /index.html;
}
# Proxy /api to backend — same origin, no CORS needed
location /api/ {
proxy_pass http://localhost:3000/api/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}Fix Wildcard CORS Misconfiguration
Preflight OPTIONS requests failingIf you set Access-Control-Allow-Origin: * and still see CORS errors, the issue is often a missing response to the browser's OPTIONS preflight request. Your backend must handle OPTIONS explicitly.
// Option A: cors() middleware handles OPTIONS automatically
app.use(cors({ origin: "https://yourapp.com" }))
// Option B: manual preflight handler
app.options("*", (req, res) => {
res.setHeader("Access-Control-Allow-Origin", "https://yourapp.com")
res.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS")
res.setHeader("Access-Control-Allow-Headers", "Content-Type,Authorization")
res.status(204).send()
})Prevention
- Use relative API paths (/api/...) in your frontend code — never hardcode localhost:3000
- Configure Vite's server.proxy for development so the team never hits CORS errors locally
- Set up explicit CORS origins in your backend for both development and production domains
- Never use Access-Control-Allow-Origin: * with credentials: true — it will be blocked by the browser
- Handle OPTIONS preflight requests in your backend — use a CORS middleware library rather than manual headers
- Use Nginx or platform rewrites in production to route /api calls through the same origin as your frontend
Frequently Asked Questions
Why am I getting a CORS error in my Vite app?+−
How do I fix CORS in Vite development?+−
Does Vite have a built-in CORS fix?+−
Why does my CORS error say 'No Access-Control-Allow-Origin header'?+−
CORS works without credentials but fails with cookies — why?+−
How do I fix CORS in Vite production build?+−
Need Expert Help?
We Build & Deploy Vite + React Applications
Softplix engineers build production-ready Vite applications with proper API architecture. Struggling with CORS, deployment, or backend integration? Let us help.
Talk to an Engineer