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 Rollup behavior that differs from the dev server. 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 Rollup builds behaving differently from the dev server. 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 `vite` dev but fails in `vite 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 Rollup tree-shaking or resolution that the dev server skips.
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 | Rollup vs esbuild differences | Reproduce with `vite build` + `vite preview` and read the Rollup 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 Rollup warning's module pathConfig fixes
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'],
},
});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 preview` to 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
Official sources
Backs the breaking-change and migration-step claims.
Backs the breaking-change and migration-step claims.