Vite "Rollup failed to resolve import" - Fix vite build in CI
vite build uses Rollup, and Rollup could not resolve a module the dev server tolerated - usually a genuinely missing dependency, an over-broad external, or a deep subpath the package does not export.
What this error means
The dev server runs fine, but vite build fails with [vite]: Rollup failed to resolve import "<module>" from "<file>". It surfaces only at build time, which is why CI catches what local vite dev did not.
[vite]: Rollup failed to resolve import "lodash/debounce" from "src/hooks/useSearch.ts".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`Common causes
Dependency missing from package.json
The import worked in dev via a hoisted transitive dependency, but it is not a declared dependency, so a clean npm ci build cannot resolve it.
Module incorrectly treated as external
A build.rollupOptions.external entry (or a library build config) excludes a module the app actually needs bundled.
Deep import path that does not exist
A subpath import (pkg/some/internal) the package does not export, or that exists only under a different path, resolves in dev tooling but not in the Rollup build.
How to fix it
Add the dependency explicitly
Declare every imported package so a clean install resolves it.
npm install lodash
npm ls lodash # confirm it's a direct dependency, not just transitiveFix or remove an over-broad external
Only externalize modules you truly provide at runtime (e.g. peers in a library build); otherwise let Vite bundle them.
// vite.config.ts (library build)
build: { rollupOptions: { external: ['react', 'react-dom'] } }How to prevent it
- Run
vite buildin CI (not justvite dev) so resolution gaps fail early. - Declare all imports as direct dependencies; do not rely on hoisting.
- Keep
rollupOptions.externalminimal and intentional.