This Vite error occurs when the bundler cannot locate an imported module, typically due to incorrect paths, missing dependencies, or misconfigured path aliases.
This error appears when Vite's import resolution system fails to find a module you're trying to import in your React application. Vite uses a sophisticated module resolution system that checks multiple locations and file extensions, but when it exhausts all possibilities, it throws this error asking "Does the file exist?" The error can manifest in different forms depending on the plugin involved: "[plugin:vite:import-analysis] Failed to resolve import" during development or "[vite]: Rollup failed to resolve import" during builds. Both indicate the same underlying issue - Vite cannot map your import statement to an actual file on disk. This is particularly common when migrating from Create React App to Vite, using custom path aliases, or working with monorepos where module resolution paths differ from standard Node.js conventions.
First, confirm the imported file actually exists at the expected location. Check for common mistakes:
// ❌ Wrong - filename case mismatch
import Button from './components/button'; // file is Button.tsx
// ✅ Correct - exact case match
import Button from './components/Button';
// ❌ Wrong - missing file extension for non-standard imports
import config from './config'; // Vite may not find config.ts
// ✅ Correct - explicit extension
import config from './config.ts';Remember that Linux and macOS are case-sensitive, even if your Windows development environment isn't.
If importing a package from node_modules, ensure it's installed:
# Check if package exists
npm list package-name
# Install if missing
npm install package-name
# Or for clean reinstall
rm -rf node_modules package-lock.json
npm installFor React-specific packages, verify React itself is installed:
npm install react react-domVite caches module resolution to improve performance. After file moves or renames, clear the cache:
# Remove Vite cache directory
rm -rf node_modules/.vite
# Restart dev server
npm run devYou can also try Vite's force flag to skip cache:
vite --forcePath aliases require configuration in TWO places - Vite for bundling and TypeScript for type checking.
vite.config.ts:
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
'@components': path.resolve(__dirname, './src/components'),
},
},
});tsconfig.json:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"],
"@components/*": ["./src/components/*"]
}
}
}Alternatively, use the vite-tsconfig-paths plugin to automatically sync:
npm install -D vite-tsconfig-paths// vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import tsconfigPaths from 'vite-tsconfig-paths';
export default defineConfig({
plugins: [react(), tsconfigPaths()],
});When migrating from Create React App, you may need to configure the JSX runtime:
// vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [
react({
jsxRuntime: 'classic' // Use classic JSX transform
})
],
});Or ensure React is in scope for automatic runtime:
// At the top of JSX files
import React from 'react';Some modern packages are ESM-only and cannot be imported with require(). Check the error message for clues:
// ❌ Wrong - using CommonJS
const module = require('esm-only-package');
// ✅ Correct - using ESM
import module from 'esm-only-package';For monorepos, explicitly include linked dependencies in Vite's optimization:
// vite.config.ts
export default defineConfig({
optimizeDeps: {
include: ['linked-package-name']
}
});File Extension Resolution:
Vite checks extensions in this order: ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json']. While it's tempting to omit extensions, explicitly including them (especially for .tsx/.jsx files) reduces filesystem checks and makes imports more explicit.
Monorepo Considerations:
In monorepo setups (Yarn workspaces, pnpm workspaces, Lerna), ensure linked packages export proper ESM and are included in optimizeDeps.include. Symlink resolution can also cause issues - set resolve.preserveSymlinks: false in vite.config.ts if needed.
Conditional Exports:
Packages using conditional exports in package.json may need additional resolve.conditions configuration. Check the package documentation for required conditions.
Case Sensitivity:
Even if your development OS is case-insensitive (Windows/macOS), your production environment (Linux) likely isn't. Use consistent casing everywhere to avoid "works on my machine" issues.
Version-Specific Bugs:
Some Vite versions (notably 5.1.0-beta.4 and 7.0.5) had regressions in import resolution. If you encounter persistent issues after trying all solutions, check the Vite changelog and consider upgrading or downgrading.
Prop spreading could cause security issues
Prop spreading could cause security issues
Error: error:0308010C:digital envelope routines::unsupported
Error: error:0308010C:digital envelope routines::unsupported
React Hook "useEffect" is called conditionally. React Hooks must be called in the exact same order in every component render.
React Hook useEffect placed inside a condition
Hook can only be called inside the body of a function component
Hook can only be called inside the body of a function component
Rollup failed to resolve import during build
How to fix "Rollup failed to resolve import" in React