error Couldn't find package: corepack enable falls back to Yarn 1.x, ignoring your Berry lockfile
corepack enable alone falls back to Yarn 1.x, which ignores a Berry lockfile and re-resolves everything to the latest tags. The mechanism, the fix and the check.
error Couldn't find packageIn short
- Symptom
- The container breaks during install with
Couldn't find package … required by …. Or, worse, it builds silently with versions that are not the ones in the lockfile. - Real cause
corepack enableonly installs shims. The version actually used is picked on the first call, by readingpackageManagerin the current directory. Without that field, or beforeCOPY package.json, Corepack falls back to Yarn 1.x, which cannot read a Berry lockfile and re-resolves the whole tree.- Fix
- Pin
packageManagerat the root, copy the metadata before enabling, then runcorepack enable && corepack installandyarn install --immutable. - Prevention
- A CI check that compares
yarn --versionto thepackageManagerfield, and one imperative line in the repository instruction file.
- Updated
- 2026-10-14
- Read time
- 8 min
- Access
- open
The answer, in three lines
corepack enable does not activate your project's version: it installs shims, and the version is
only picked later, by reading packageManager in the current directory. If there is nothing to
read, Corepack falls back to Yarn 1.x. Yarn 1.x cannot read a Berry lockfile, and it
re-resolves the whole dependency tree to the latest published tags, sometimes without printing
anything.
The fix has three parts: packageManager pinned at the root, the metadata COPY lines placed
before enabling, and corepack enable && corepack install followed by
yarn install --immutable.
The symptom
The failure takes two forms, and that is why the diagnosis is slow.
The first is a loud failure. The container builds, then breaks during install or at the first
require:
$ docker compose build app
...
=> [4/6] RUN yarn install
error Couldn't find package "@mon-app/shared-types@workspace:*" required by "@mon-app/backend@workspace:*"
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
ERROR: process "/bin/sh -c yarn install" did not complete successfully: exit code: 1
The second is a silent failure, and it is the expensive one. Nothing fails. The build passes, the image ships to production, and a dependency is not at the version the lockfile describes:
$ docker compose run --rm app node -e "console.log(require('date-fns/package.json').version)"
4.3.0
$ grep -A1 '"date-fns@npm:\^4.1.0"' yarn.lock
"date-fns@npm:^4.1.0":
version: 4.1.0
The lockfile says 4.1.0, the container has 4.3.0, and nobody changed anything.
The wrong diagnosis
- "The Docker cache is stale." You rerun with
--no-cache, the failure comes back, and twenty minutes of build time are gone for nothing. - "The lockfile is corrupt or was committed wrong." You regenerate it locally, where everything works, because the right Yarn has been active there for months. The regenerated lockfile is identical. You conclude that it is fine, which is true, and you look elsewhere.
- "It's a Yarn workspaces problem." The error message mentions
workspace:*, so you suspect the workspace declaration, and you spend an hour in the monorepo documentation.
What you do not think to do is ask the container which version of Yarn it is running. Nothing
prompts you to: the RUN corepack enable line is right there, you wrote it on purpose, and it
looks like it is doing its job.
What is actually happening
corepack enable does not activate your project's version: it installs shims.
corepack enable puts yarn, pnpm and npm shims on the PATH, and does nothing else. The
version actually used is only picked later, on the first call, by reading the packageManager
field of the package.json in the current directory. Two situations break this inside a
Dockerfile.
- The
package.jsonis not there yet. IfRUN corepack enablesits above theCOPY package.json, as it does in nearly every Dockerfile written from memory, there is nothing to read. Corepack falls back to its default version, which is Yarn 1.x. - There is no
packageManagerfield at all. A monorepo that never pinned its package manager version works fine locally, where the developer installed a global Yarn Berry by hand months ago. In a clean image, it falls back to Yarn 1.x.
And Yarn 1.x cannot read a Berry lockfile. It does not refuse it and does not report it: the
format is unknown to it, it acts as if the file did not exist, and it re-resolves the entire
dependency tree to the latest versions compatible with your semver ranges. ^4.1.0 becomes 4.3.0,
which is exactly what the lockfile was there to prevent.
The workspace:* protocol is Berry notation. Yarn 1.x does not know it, hence the
Couldn't find package ... required by .... When the monorepo does not use that protocol, no error
message appears at all: that is the silent form, and the drift ships to production.
The fix
Two rules, in this order.
Step 1: Pin the version in the root package.json
Without that field, no Corepack command can know which version to install.
{
"name": "mon-app",
"packageManager": "[email protected]"
}
Step 2: Copy the metadata BEFORE enabling, and use corepack install
FROM node:22-alpine
WORKDIR /workspace
-RUN corepack enable
-RUN corepack prepare yarn@latest --activate
-
-COPY package.json yarn.lock .yarnrc.yml ./
+# 1. metadata first — this is where corepack install reads `packageManager`
+COPY package.json yarn.lock .yarnrc.yml ./
+COPY .yarn ./.yarn
+
+# 2. `corepack install` (no argument) downloads and activates EXACTLY the
+# version declared in packageManager. `corepack enable` alone does not.
+RUN corepack enable && corepack install
-RUN yarn install
+RUN yarn install --immutable
--immutable is the second half of the fix: it makes the install fail if the lockfile would
have to change. Without it, drift is still possible for another reason (an added resolutions, a
widened range), and it goes back to being silent.
If the project copies .yarnrc.yml, copy it before yarn install. A .yarnrc.yml that arrives
after the install means the install ran with Yarn Berry's defaults, that is, Plug'n'Play instead of
nodeLinker: node-modules. The build passes, and the container breaks at startup, for an
unexpected reason. I reproduced it on 24/09/2026 (Yarn 4.15, Vite 5.4): the refusal does not come
from Vite. At launch, Yarn reads the now-present .yarnrc.yml, looks for a node_modules state
the install never produced, and stops on Couldn't find the node_modules state file.
Check (pass or fail)
docker compose run --rm app yarn --version
# must print exactly the value of `packageManager`. If it prints 1.22.x, that is the failure.
docker compose run --rm app yarn install --immutable
# must exit 0. A non-zero exit means the resolved tree does not match the lockfile.
Prevention
Three places are enough:
- In CI, a three-line job compares
yarn --versionto thepackageManagerfield and fails on any mismatch. It also catches the Dockerfile somebody adds later. - In the repository instruction file, one imperative line: "Package manager: Yarn 4.x, activated
with
corepack enable && corepack install. Nevercorepack prepare … --activate. Every install inside a container uses--immutable." It is the most useful line in the file: it stops an agent from "fixing" a build by replacing the command with the one it saw most often in its training data. - When you review a Dockerfile, ask one question: do the metadata
COPYlines come before the package manager is enabled?
Why this matters with agents
This failure gets worse with a fleet of agents, for three reasons.
An agent writes the Dockerfile from memory. RUN corepack enable at the top of the file, right
after the FROM, is the most common pattern on the public internet, and therefore in the models.
An agent will produce the broken version on its own, and it builds. Nothing in the output flags a
problem.
The silent form gets through verification. Tests pass, since they run against a dependency tree that simply is not the one in the lockfile. Code review does not see the failure either: the Dockerfile diff is correct line by line. The failure only shows up when you compare the installed version to the locked version, and no agent does that unless you ask.
In parallel, you can no longer tell where the failure came from. Six agents, six branches, and a build broken at integration time: which branch introduced the version drift? They all resolved differently, because they installed at different times, against different npm tags. The more agents you run, the more different resolutions you get, and no single branch can be named as the source of the drift.
What this entry does not cover
The case where .yarnrc.yml arrives after yarn install and switches the project to Plug'n'Play
has its own entry.
Where this entry comes from
An incident I hit on a personal Yarn 4 monorepo, between February and August 2026. The entry is anonymised: the repository, the host names and the original paths do not appear here.
This entry is part of a notebook of 27 production failures hit between February and September 2026. Some are public and free: see the notebook.
The others are published as they are written, along with the modules and their exercises, as part of the paid product. In that product, the modules are in English; the exercises and the other entries are in French. Nothing on this page is a teaser for it.