This error occurs in Vite when you try to import a module using default import syntax, but the module only exports named exports. The fix involves matching your import syntax to the actual export type.
This error appears when there's a mismatch between how a module exports its values and how you're importing them in your Vite React project. Vite strictly enforces ES module standards, which distinguish between default exports (`export default`) and named exports (`export { name }`). When a module uses named exports but you try to import it as a default export (or vice versa), Vite's development server or build process will throw this error. This is more common in Vite than older bundlers because Vite leverages native ES modules during development and performs stricter module resolution. The error often surfaces after adding a new dependency, creating a new component file, or when Vite's hot module replacement (HMR) cache becomes inconsistent with your actual code.
Examine the error message to find which module is causing the issue. The error will show the exact file path:
The requested module '/src/components/Button.tsx' does not provide an export named "default"Navigate to the file where you're importing this module and check your import statement.
Open the file being imported and verify how it exports its values.
Named export (no default):
// components/Button.tsx
export function Button() {
return <button>Click me</button>;
}Default export:
// components/Button.tsx
export default function Button() {
return <button>Click me</button>;
}The export style determines which import syntax you must use.
Update your import statement to match the export type.
If the module uses named exports, use named import:
// ✅ Correct for named export
import { Button } from './components/Button';If the module uses default export, use default import:
// ✅ Correct for default export
import Button from './components/Button';You can also import both if the module has both:
import Button, { ButtonProps } from './components/Button';If the import/export syntax is correct but the error persists, Vite's cache may be stale.
Delete the cache directory:
rm -rf node_modules/.viteRestart the Vite dev server:
npm run dev
# or
yarn devThis forces Vite to rebuild its dependency cache and can resolve HMR-related inconsistencies.
Some npm packages use CommonJS format, which can cause import issues in Vite. If you're importing from a third-party package:
For CommonJS modules, always use default import:
// ✅ Correct for CommonJS
import pkg from 'commonjs-package';
const { specificExport } = pkg;Or configure Vite to handle CommonJS properly:
// vite.config.js
import { defineConfig } from 'vite';
export default defineConfig({
optimizeDeps: {
include: ['problematic-commonjs-package']
}
});Vite recommends explicit file extensions for non-JavaScript imports. If you're importing a Vue, Svelte, or other component type:
Without extension (may fail):
import MyComponent from './components/MyComponent';With extension (more reliable):
import MyComponent from './components/MyComponent.vue';
// or
import MyComponent from './components/MyComponent.tsx';Check your vite.config.js resolve.extensions configuration if automatic resolution is expected.
Understanding ES Module Strictness:
Vite uses native ES modules in development, which means it enforces strict module semantics that older bundlers like Webpack might have been more lenient about. This is why code that worked in Create React App or Webpack projects might fail in Vite.
Barrel Exports and Re-exports:
If you're using barrel exports (index.js files that re-export multiple modules), ensure the re-export syntax matches the original export type:
// ✅ Re-exporting default as default
export { default as Button } from './Button';
// ✅ Re-exporting named as named
export { Button } from './Button';
// ❌ Mismatch - will cause errors
export { Button as default } from './Button'; // if Button is a named exportTypeScript Declaration Files:
Sometimes TypeScript's .d.ts files might declare exports differently than the actual JavaScript implementation. If you're using a library with type issues, check both the types and the actual runtime code.
Vite Plugin for CommonJS:
For projects heavily dependent on CommonJS libraries, consider using @originjs/vite-plugin-commonjs which provides better CommonJS interop, though native ES modules are preferred for better tree-shaking and performance.
React Hook useCallback has a missing dependency: 'variable'. Either include it or remove the dependency array react-hooks/exhaustive-deps
React Hook useCallback has a missing dependency
Cannot use private fields in class components without TS support
Cannot use private fields in class components without TS support
Cannot destructure property 'xxx' of 'undefined'
Cannot destructure property of undefined when accessing props
useNavigate() may be used only in the context of a <Router> component.
useNavigate() may be used only in the context of a Router component
Cannot find module or its corresponding type declarations
How to fix "Cannot find module or type declarations" in Vite