Solving Local CORS Issues with Vite's Dev Server Proxy
Vite's dev server proxy forwards API requests to your backend during development, eliminating CORS errors. Configure server.proxy with target and changeOrigin, verify with curl, then handle production API URLs via env vars or a reverse proxy.
15 Jul 2026, 19:44 UTC

The Problem: CORS Blocks Local API Calls
When your frontend runs on http://localhost:5173 and your backend API lives on http://localhost:3000, browsers block the cross-origin requests. You could enable CORS on the backend, but that adds configuration surface area and doesn't mirror production where both often share a domain. Vite's built-in dev server proxy eliminates this friction by forwarding selected paths to your backend during development.
How the Proxy Works
The server.proxy option in vite.config.ts tells Vite's development server to intercept requests matching a path prefix and forward them to a target URL. The proxy only runs during vite dev; it is completely stripped from production builds. This means you configure it once for local development and handle API routing differently for production.
Key options include:
- target: The backend URL to forward requests to (e.g.,
http://localhost:3000). - changeOrigin: Rewrites the
Hostheader to match the target, preventing backend routing errors when the backend uses virtual hosting. - pathRewrite: Transforms the request path before forwarding, useful for stripping or adding path segments.
Worked Example: Proxying /api to a Node Backend
Assume a Vite + React app on port 5173 and an Express API on port 3000. The frontend calls fetch('/api/users'). Without a proxy, the browser sends the request to localhost:5173/api/users and the dev server returns 404. With the proxy, the request is forwarded to localhost:3000/api/users.
// vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
server: {
proxy: {
'/api': {
target: 'http://localhost:3000',
changeOrigin: true,
// optional: rewrite /api/foo -> /foo if your backend doesn't use /api prefix
// pathRewrite: { '^/api': '' },
},
},
},
})
Start the dev server with npm run dev (or vite dev). Open the browser's Network tab, trigger the API call, and verify the request shows Request URL: http://localhost:5173/api/users but the response comes from the backend on port 3000. The Host header seen by the backend will be localhost:3000 because of changeOrigin: true.
Verifying the Proxy Behavior
Run a quick curl against the dev server to confirm header rewriting:
curl -v http://localhost:5173/api/health 2>&1 | grep -i host
# Expected: Host: localhost:3000 (not localhost:5173)
If the backend logs show the correct host and path, the proxy is working. If you see CORS errors in the browser console, double-check that the request path matches the proxy key exactly (including leading slash) and that the backend is running on the target port.
Trade-off: Dev-Only Configuration
The proxy configuration disappears when you run vite build. Production bundles must know the real API base URL. Common approaches:
- Use an environment variable like
VITE_API_BASE_URLand read it viaimport.meta.env.VITE_API_BASE_URLin your fetch calls. - Deploy behind a reverse proxy (nginx, Cloudflare, Vercel) that routes
/api/*to the backend, mirroring the dev proxy behavior.
This separation is intentional: it keeps the production build free of dev-only tooling and forces you to define the actual deployment topology.
Closing Checklist
- Add
server.proxytovite.config.tswithtargetandchangeOrigin: true. - Run
vite devand confirm Network tab requests reach the backend. - Use
curl -vto verify theHostheader is rewritten. - For production, set
VITE_API_BASE_URLat build time or configure a reverse proxy.
With this setup, you develop against a local backend without CORS headaches, and the transition to production is a single environment variable or reverse-proxy rule away.