Vite optimizeDeps "Failed to resolve entry for package" - Fix in CI
Vite pre-bundles dependencies with esbuild and could not find a usable entry point for one of them. The package's exports/main map does not point at a file Vite can load, or the dependency must be explicitly included or excluded from optimization.
What this error means
The dev server or vite build fails during dep optimization with Failed to resolve entry for package "<pkg>". The package may have incorrect main/module/exports specified in its package.json. It is deterministic and names the package.
Failed to resolve entry for package "some-lib". The package may have incorrect
main/module/exports specified in its package.json.
at packageEntryFailure (vite/dist/node/chunks/dep.js)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
Broken package entry map
The dependency declares an exports/main/module that points at a missing file or a condition Vite does not resolve, so pre-bundling has no entry to start from.
Dependency needs explicit include/exclude
A package shipped as CJS, or one that re-exports many internal modules, may need to be listed in optimizeDeps.include (to be pre-bundled) or exclude (to be left alone) for Vite to handle it correctly.
How to fix it
Force-include or exclude the dependency
Tell Vite how to treat the problematic package during optimization.
// vite.config.ts
export default defineConfig({
optimizeDeps: {
include: ['some-lib'], // pre-bundle a CJS/awkward dep
exclude: ['some-esm-only-lib'], // skip optimization for a pure-ESM dep
},
})Work around a broken upstream entry
- Inspect the dependency's
package.jsonexports/mainand confirm the target file exists. - If the package is genuinely broken, alias the import to the real file, or pin a version with a correct entry map.
- Report the broken entry upstream so the fix is durable.
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
- Pin dependency versions so an entry-map regression cannot drift in.
- Use
optimizeDeps.include/excludefor known-awkward packages. - Run
vite buildin CI so pre-bundle failures surface before deploy.