Skip to content
Latchkey

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
[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.

Terminal
# 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 build

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.

Terminal
npm install lodash
npm ls lodash   # confirm it's a direct dependency, not just transitive

Fix 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
// 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-node and in engines. A bundler that resolves native bindings will pick a different prebuilt binary across majors.
  • Delete node_modules locally and reinstall from the lockfile before concluding the runner is at fault; most "works locally" reports are stale local state.
  • Set CI=true locally 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-size or move to a larger runner rather than searching the bundler config.

How to prevent it

  • Run vite build in CI (not just vite dev) so resolution gaps fail early.
  • Declare all imports as direct dependencies; do not rely on hoisting.
  • Keep rollupOptions.external minimal and intentional.

Frequently asked questions

What causes Vite "Rollup failed to resolve import"?
There are 3 common causes: dependency missing from package.json, module incorrectly treated as external, and deep import path that does not exist. 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.
How do I fix Vite "Rollup failed to resolve import"?
There are 2 fixes depending on which cause you have: add the dependency explicitly and fix or remove an over-broad external. Work through them in order, since the first is the most common.
What does Vite "Rollup failed to resolve import" actually mean?
The dev server runs fine, but vite build fails with [vite]: Rollup failed to resolve import "<module>" from "<file>".
How do I stop Vite "Rollup failed to resolve import" happening again?
Run vite build in CI (not just vite dev) so resolution gaps fail early. The prevention section lists 3 changes that keep it from recurring.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card