Vite "[vite]: Rollup failed to resolve import" - Fix Prod Builds
This is the production-build cousin of "Failed to resolve import". vite build uses Rollup, and Rollup could not resolve a module that the dev server tolerated - usually a genuinely missing dependency or one Rollup treated as external.
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`Diagnose it: is it resolution, transform, or memory?
Bundler failures in CI fall into three families and the error text often points at the wrong one. A module that resolves on your machine and not on the runner is nearly always case sensitivity or a missing optional dependency; a transform error is a config or version mismatch; and an unexplained kill with no stack is the out-of-memory reaper, not a build error at all.
# 1. resolution: does the file exist with EXACTLY that case?
git ls-files | grep -i "the/imported/path"
# 2. transform: what versions is CI actually resolving?
npm ls webpack vite rollup esbuild typescript 2>/dev/null | head -20
# 3. memory: was it killed rather than failed?
# exit 137 = SIGKILL (OOM). Nothing in the bundler log will explain it.
node --max-old-space-size=4096 node_modules/.bin/vite buildCommon 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) is excluding a module that the app actually needs bundled.
Deep import path that does not exist
A subpath import (pkg/some/internal) that 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. peer deps in a library build); otherwise let Vite bundle them.
// vite.config.ts (library build)
build: { rollupOptions: { external: ['react', 'react-dom'] } }Make the build reproducible before you debug it
- Pin the Node major in
setup-nodeand inengines. A bundler that resolves native bindings will pick a different prebuilt binary across majors. - Delete
node_moduleslocally and reinstall from the lockfile before concluding the runner is at fault; most "works locally" reports are stale local state. - Set
CI=truelocally to reproduce. Several toolchains change behaviour under it, including treating warnings as errors. - Exit code 137 is an out-of-memory kill. Raise
--max-old-space-sizeor move to a larger runner rather than searching the bundler config.
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.