# bun install: Integrity check failed for package after migrating from npm

TL;DR: Bun's lockfile and `node_modules` disagree about a package's contents - leftover npm-installed files are tripping bun's integrity check. Delete `node_modules` and the stale lockfiles, then run a fresh `bun install`. The error means cached files do not match what the lockfile expects, not that the package is corrupt.

```text
bun install: Integrity check failed for package after migrating from npm
```

## Steps

1. Clear the mixed state: `rm -rf node_modules`, then delete the stale lockfiles - `bun.lockb` and/or `bun.lock`, plus any leftover `package-lock.json` from the npm era.
   Expected: listing the directory shows none of those files remain.
2. Fresh install: `bun install`.
   Expected: install completes with no integrity errors and writes a new bun lockfile.
3. Prove the tree is consistent: `bun install --frozen-lockfile`.
   Expected: completes cleanly, confirming lockfile and modules agree.
4. If one package still fails, clear bun's download cache (`bun pm cache rm`) and reinstall.
   Expected: the package downloads fresh and passes the integrity check.

## Use this when

- Integrity check failures appear right after switching the project from npm to bun.
- Both `package-lock.json` and a bun lockfile exist (or existed) in the repo.
- `node_modules` was originally installed by npm, not bun.

## Not for this skill when

- Integrity failures on a long-running bun project with no migration involved - likely a corrupt cache or registry issue, investigate before reinstalling.
- The failure names a checksum mismatch from a private registry - verify the registry, do not just reinstall.

## Variant phrasings

- bun install integrity check failed after switching from npm
- bun lockfile integrity error npm migration
- bun install fails on package migrated project

## Why it happens

npm and bun record integrity hashes differently and lay out `node_modules` differently. A tree installed by npm, checked against a lockfile bun wrote (or vice versa), makes the integrity check compare against files bun never produced - so it fails.

## Edge cases

- If CI caches `node_modules` across the npm-to-bun switch, bust the cache key or the old tree poisons every build.
- Keep exactly one lockfile in the repo going forward; two lockfiles drifting apart recreates this bug.
- Run `bun install --frozen-lockfile` in CI so lockfile drift fails fast instead of shipping a broken tree.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_wfGVsJWw6cgZsTjYMaeqsQ
