[SOLVED] Vite Error: Failed to load url / Dynamic import cannot load CommonJS module
When developing frontend projects under Vite (React, Vue, Svelte), developers frequently trigger console exceptions: [vite] Internal server error: Failed to load url /src/... (does it exist?) or TypeError: Dynamic import cannot load CommonJS module. Because Vite serves unbundled native ES Modules in development, legacy CommonJS libraries (require / module.exports) collide with the browser loader.
Quick Diagnostics
Step-by-Step Solution
-
1
Step 1: Pre-Bundle CommonJS Packages via optimizeDeps
Instruct Vite's esbuild pre-bundler to convert the offending package into standard ESM:
JAVASCRIPT// vite.config.js import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; export default defineConfig({ plugins: [react()], optimizeDeps: { include: ['legacy-cjs-package'] } }); -
2
Step 2: Migrate Arbitrary Dynamic imports to import.meta.glob
Vite cannot parse non-static dynamic templates like
import(./views/${page}.jsx). Migrate toimport.meta.glob:JAVASCRIPT// Replace dynamic string interpolation const views = import.meta.glob('./views/*.jsx'); export async function loadView(viewName) { const loader = views[`./views/${viewName}.jsx`]; if (loader) { const module = await loader(); return module.default; } throw new Error('View component not found'); } -
3
Step 3: Clear Stale Vite Pre-Bundle Artifacts
Vite caches optimized packages inside
node_modules/.vite. Clear this directory when adjusting bundler rules:BASH# Remove cache folder rm -rf node_modules/.vite # Launch Vite forcing fresh optimization npx vite --force
Frequently Asked Questions
Why did this package work seamlessly under Webpack?
Webpack bundles all files ahead of time through polyfilled CommonJS runtimes. Vite serves native ESM directly to the browser for instant HMR without bundling overhead.
How do I fix "require is not defined" inside Vite client code?
Convert require() calls to static import declarations or standard await import() statements.
Prevention Advice
Recommended security practices:
- Audit external dependencies on npm for standard
"exports"and ESM compliance. - Ensure your
package.jsondefines"type": "module".