Skip to content
Latchkey

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.

Terminal
# node_modules was restored from an npm-based cache
error: Cannot find module "zod" from "src/index.ts"
# bun.lockb and the restored tree disagree

Common 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

  1. Remove any restored node_modules before installing with Bun.
  2. Run bun install so Bun builds the tree it expects.
  3. Do not mix npm/yarn/pnpm installs in the same job.
Terminal
rm -rf node_modules
bun install --frozen-lockfile

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

.github/workflows/ci.yml
- 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.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →