Bun vs npm node_modules mismatch in CI
A node_modules directory produced by a different package manager (or cached from an npm job) does not match how Bun lays out and resolves dependencies, so Bun sees a broken or incomplete tree.
What this error means
After a cached or npm-created node_modules, Bun commands fail with missing packages or unexpected versions, or Bun reports the tree is inconsistent with bun.lockb.
# node_modules was restored from an npm-based cache
error: Cannot find module "zod" from "src/index.ts"
# bun.lockb and the restored tree disagreeCommon causes
A node_modules created by another package manager
npm, yarn, or pnpm arrange node_modules differently (symlinks, hoisting) than Bun, so a mixed tree does not satisfy Bun's resolver.
A stale node_modules cache across managers
Restoring a node_modules cache keyed on an npm lockfile into a Bun job leaves a tree Bun did not build.
How to fix it
Let Bun own the install
- Remove any restored
node_modulesbefore installing with Bun. - Run
bun installso Bun builds the tree it expects. - Do not mix npm/yarn/pnpm installs in the same job.
rm -rf node_modules
bun install --frozen-lockfileCache the Bun store, not node_modules
Cache ~/.bun/install/cache keyed on bun.lockb so restores are compatible with Bun rather than caching a foreign node_modules.
- uses: actions/cache@v4
with:
path: ~/.bun/install/cache
key: bun-${{ hashFiles('bun.lockb') }}How to prevent it
- Use one package manager per project; let Bun own node_modules.
- Cache the Bun install cache keyed on bun.lockb, not node_modules.
- Remove restored node_modules before bun install.