Skip to content
Latchkey

Yarn Berry "Cache path does not exist" / Hardened Mode - Fix in CI

Yarn Berry CI installs run with --immutable, which forbids changes to the lockfile and (with a committed offline cache) expects every package to already be present. A drifted lockfile or an incomplete cache fails the install.

What this error means

yarn install --immutable fails because the lockfile would change, or because a cache entry it expected is missing. It is deterministic - the committed state simply does not satisfy the immutable contract.

Yarn output
➤ YN0028: The lockfile would have been modified by this install,
which is explicitly forbidden.
➤ YN0000: Failed with errors in ...s
# or, with zero-installs cache committed:
Error: cache path does not exist for some-pkg@npm:1.2.3

Common causes

The lockfile is out of sync under --immutable

A dependency change that was not committed to yarn.lock means the immutable install would have to modify it, which YN0028 forbids.

An incomplete committed offline cache (zero-installs)

With zero-installs, the .yarn/cache is committed. If a package’s cache artifact is missing, the immutable install cannot fetch it and fails.

How to fix it

Regenerate and commit lockfile (and cache)

Run a normal install locally to update yarn.lock (and the committed cache for zero-installs), then commit.

Terminal
yarn install
git add yarn.lock .yarn/cache
git commit -m "chore: sync yarn lockfile and cache"

Confirm the cache strategy

  1. For zero-installs, ensure .yarn/cache is fully committed (not gitignored).
  2. For non-zero-installs, cache .yarn/cache in CI keyed on yarn.lock instead of committing it.
  3. Run yarn install after any dependency change so the lockfile and cache stay consistent.

How to prevent it

  • Commit yarn.lock with every dependency change.
  • Keep CI on --immutable and fix drift in the repo.
  • Match the cache approach (zero-installs vs CI cache) consistently.

Related guides

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