Fix Vite build errors
Most Vite errors trace to one of four causes: env vars read the old way, CommonJS-to-ESM interop, dependency pre-bundling, or production-bundler behavior that differs from the dev server (the bundler is Rolldown as of Vite 8). Diagnose by which phase fails.
Last verified · Updated May 22, 2026
Vite errors usually fall into four buckets: import.meta.env reads returning undefined, CommonJS interop ('does not provide an export named default'), dependency pre-bundling needing optimizeDeps, and production builds behaving differently from the dev server. On Vite 8 both dev pre-bundling and the production build run on Rolldown, so the dev-vs-build gap is smaller than under the old esbuild + Rollup split — but it can still appear. Identify the failing phase first — dev server vs production build — then match the bucket.
Symptoms
- 'does not provide an export named default' when importing a CommonJS package.
- import.meta.env.VITE_X is undefined at runtime.
- Works in
vitedev but fails invite build(or vice versa). - SSR errors about an externalized dependency.
Likely causes
- Default-vs-named import mismatch against a CommonJS module's interop shape.
- An env var missing the VITE_ prefix, so it is not exposed to client code.
- A CommonJS dependency that needs to be force pre-bundled via optimizeDeps.include.
- ssr.noExternal not set for a package that must be bundled for the server build.
- Production-only Rolldown tree-shaking or resolution that the dev server skips (Vite 8 builds with Rolldown).
Error to fix
| Error | Likely cause | Fix |
|---|---|---|
| no export named 'default' | CommonJS interop | Use a namespace/default import or add to optimizeDeps.include |
| import.meta.env.X undefined | Missing VITE_ prefix | Rename the env var to VITE_X and restart dev |
| Dev works, build fails | Production-bundler (Rolldown) differences | Reproduce with vite build + vite preview and read the build warning |
| SSR externalized error | ssr.noExternal not set | Add the package to ssr.noExternal |
Diagnostic commands
# Reproduce the production build (not just the dev server)
npx vite build
npx vite preview
# Read the first build (Rolldown) warning's module pathConfig fixes
optimizeDeps and SSR externalization
export default defineConfig({
optimizeDeps: {
// Force-prebundle a CommonJS dep that errors under native ESM.
include: ['legacy-cjs-package'],
},
ssr: {
// Bundle (don't externalize) a dep for the server build.
noExternal: ['esm-only-package'],
},
});AI debugging prompt
I'm getting a Vite error: <paste the error and whether it happens in `vite` dev or `vite build`>. Identify which bucket it falls in — import.meta.env, CommonJS interop, optimizeDeps pre-bundling, or SSR externalization — then propose the smallest config or import change that fixes it. Prefer optimizeDeps.include / ssr.noExternal / an import-style fix over disabling features.Safety: Do not disable the production build or broadly mark deps external to silence the error — fix the specific interop or env issue.
Safe fix workflow
- Confirm which phase fails: dev server or production build.
- For interop errors, fix the import style or add the package to optimizeDeps.include.
- For env errors, rename the variable with a VITE_ prefix and restart the dev server.
- For SSR errors, add the package to ssr.noExternal.
- Re-run
vite build+vite previewto confirm the production path is clean.
Escalation checklist
- The error reproduces only in the production build, not in dev
- A dependency ships no ESM build and resists optimizeDeps
- The interop error comes from a transitive dependency you do not control
Related migration paths
- Webpack vs Vite — Next step forward
- Migrate from Jest to Vitest — Related path
- Migrate from Webpack to Vite — Adjacent migration
Official sources
- Vite Guide — vite.dev (reliability 98%)
- vitejs/vite releases — vitejs/vite (reliability 92%)