.yarnrc.yml copied after yarn install in the Dockerfile: the container dies on Usage Error: Couldn't find the node_modules state file - running an install might help
A .yarnrc.yml copied after yarn install makes Yarn Berry install in Plug'n'Play, then start in node-modules. The COPY order, the fix and the check.
Usage Error: Couldn't find the node_modules state fileAlso searched as: ".yarnrc.yml copied after yarn install Dockerfile COPY order"
In short
- Symptom
- The image builds without an error, then the container stops at startup on
Usage Error: Couldn't find the node_modules state file. In the silent variant, nothing breaks, and the project runs in Plug'n'Play without ever declaring it. - Real cause
- Yarn Berry re-reads its configuration on every command. At
yarn installtime,.yarnrc.ymlis not in the image yet: the install uses the default linker, Plug'n'Play. At startup the file is there and asks fornode-modules, whose state was never written. - Fix
- Copy
.yarnrc.ymlin the sameCOPYinstruction aspackage.jsonandyarn.lock, beforeyarn install, then check the linker inside the built image. - Prevention
- The package manager configuration is an input of the install, like the lockfile: it is copied with it, and a check reads
yarn config get nodeLinkerinside the image.
- Updated
- 2026-10-14
- Read time
- 8 min
- Access
- open
The answer, in three lines
Yarn Berry re-reads .yarnrc.yml on every command. If the file enters the image after
yarn install, the install runs with the default setting, Plug'n'Play: a .pnp.cjs, and no
node_modules. At startup the same Yarn reads nodeLinker: node-modules, looks for the state of a
node_modules that was never created, and refuses to run the script.
The fix: .yarnrc.yml in the same COPY instruction as package.json and yarn.lock, before
the install.
The symptom
The image builds without a single error, but the container stops at its first command:
$ docker run --rm front-dev
Usage Error: Couldn't find the node_modules state file - running an install might help (findPackageLocation)
$ yarn run [--inspect] [--inspect-brk] [-T,--top-level] [-B,--binaries-only] [--require #0] <scriptName> ...
$ echo $?
1
The Dockerfile that produces this output looks right line by line:
FROM node:22-alpine
RUN corepack enable
RUN corepack prepare [email protected] --activate
WORKDIR /app
COPY package.json yarn.lock ./
COPY frontend ./frontend
RUN yarn install
COPY .yarnrc.yml ./
WORKDIR /app/frontend
CMD ["yarn", "dev", "--host", "0.0.0.0"]
There is a silent variant, and it is the one I actually hit: .yarnrc.yml is copied neither before
nor after. Nothing breaks. The install runs in Plug'n'Play, so does startup, and the project runs in
a mode it never declared.
The wrong diagnosis
The message says "running an install might help". The most direct answer is to run an install again
at startup, with a yarn install in the container command, or to add a second RUN yarn install at
the end of the Dockerfile. Both make the error go away, because the second install finally sees
.yarnrc.yml. And both leave the cause in place: one useless install on every start, or two
installs per build, the first of which does nothing useful.
You do not think to look at the moment each file enters the image. The message talks about missing state, but it does not say that two Yarn commands in the same container read two different configurations.
What is actually happening
.yarnrc.yml is an input of the install, just like the lockfile. Yarn Berry re-reads it on every
call, and the linker it finds there (the way dependencies get installed) decides what the install
writes to disk.
In the Dockerfile above, two Yarn commands read two states of the filesystem:
RUN yarn installruns without.yarnrc.yml. Yarn Berry's default linker ispnp: the install writes.pnp.cjsand.pnp.loader.mjs, and nonode_modules.COPY .yarnrc.yml ./comes next, withnodeLinker: node-modules.yarn dev, at startup, reads that file. To run the script with thenode-moduleslinker, it looks for the state that linker writes tonode_modules/.yarn-state.yml. The directory does not exist:findPackageLocationfails, and the exit code is 1.
Three readings in the images rebuilt on 24/09/2026 (Node 22, Yarn 4.15.0) show the mechanism:
.yarnrc.yml | yarn config get nodeLinker | On disk | Startup |
|---|---|---|---|
| never copied | pnp | .pnp.cjs, no node_modules | starts |
| copied after the install | node-modules | .pnp.cjs, no node_modules | Usage Error, exit 1 |
| copied before the install | node-modules | node_modules/.yarn-state.yml | starts |
The first row is the silent variant. Plug'n'Play is not a failure in itself: in the reproduction,
the Vite dev server starts under it and serves its modules. The defect is elsewhere. The repository
declares node-modules, the image runs in pnp, and the first tool that cannot find its modules
through .pnp.cjs will break with a message about resolution, never about configuration. In another
personal repository, in February 2026, it was a test preprocessor that could not find its modules
under Plug'n'Play, outside Docker, with no .yarnrc.yml at all.
The fix
Step 1: Copy the configuration with the lockfile, before the install
FROM node:22-alpine
RUN corepack enable
RUN corepack prepare [email protected] --activate
WORKDIR /app
-COPY package.json yarn.lock ./
+COPY package.json yarn.lock .yarnrc.yml ./
COPY frontend ./frontend
RUN yarn install
-COPY .yarnrc.yml ./
WORKDIR /app/frontend
CMD ["yarn", "dev", "--host", "0.0.0.0"]
If the repository has a versioned .yarn/ directory (releases/, patches/, plugins/), copy it
at the same place, for the same reason.
Step 2: Check the linker inside the built image, not in the repository
docker build -t front-dev -f frontend/Dockerfile.dev .
docker run --rm --entrypoint sh front-dev -c \
'yarn config get nodeLinker && ls /app/node_modules/.yarn-state.yml'
# must print node-modules, then the path of the state file.
# "pnp", or a "No such file or directory", is the failure.
Check inside the image, because that is where the failure is: yarn config get nodeLinker run on
your machine reads the repository's .yarnrc.yml, and answers correctly whatever the order of the
COPY lines.
Prevention
- In the repository instruction file, write one imperative line: "In a Dockerfile,
package.json,yarn.lockand.yarnrc.ymlare copied in a single instruction, beforeyarn install." That form survives an agent rewriting the Dockerfile. - In CI, build the image and read
yarn config get nodeLinkerinside it: a value that differs from the repository's fails the job. This check also covers the Dockerfile somebody adds later. - In review, ask one question: is every file that
yarn installreads copied above it?
Why this matters with agents
An agent writes the COPY order from memory. COPY package.json yarn.lock ./, then
RUN yarn install, then COPY . . is a very common pattern, inherited from Yarn 1, which does not
read .yarnrc.yml. With Yarn Berry, that pattern makes the configuration arrive after the install,
and the image still builds.
The message pushes towards the wrong fix. An agent that reads "running an install might help" adds an install, the error goes away, and the task is closed. The check on the fix passes, because it only checks the symptom.
The silent variant gets through every check. A repository declared as node-modules that runs in
pnp inside its image prints nothing unusual. Only reading the configuration inside the image
shows it, and no agent does that unless you ask.
What this entry does not cover
Sometimes it is the Yarn version that is wrong inside the image, not its configuration:
corepack enable alone falls back to Yarn 1.x, which ignores both the Berry lockfile and
.yarnrc.yml. That is the neighbouring entry, and it comes from the same incident.
The binary not found at startup (command not found, exit 127), when two containers share Yarn's
install state, comes from another state file and another cause.
Where this entry comes from
An incident I hit on a personal Yarn 4 monorepo with two workspaces, fixed on 25 May 2026. The
frontend development Dockerfile installed without .yarnrc.yml, and the fix added it to the copy
that precedes the install, in the same commit as the fix of the neighbouring entry. The same
omission remained in the API Dockerfile, which ran in Plug'n'Play without declaring it; the change
that adds it there dates from September 2026, and it was not committed when this entry was written.
The entry is anonymised: the repository, the host names and the original paths do not appear here.
The trace has two gaps. The original incident is the silent variant: no error message was kept, and
the transcript of the session that fixed it no longer exists. The message in the title, the table
and the outputs on this page come from a reproduction made on 24/09/2026 on the repository tree
at the fix commit, with three Dockerfiles that differ only by where .yarnrc.yml is copied (Node 22,
Yarn 4.15.0, Vite 5.4). The error came back identical on the second run. The original note also
claimed that Vite does not start under Plug'n'Play: the reproduction contradicts it for these
versions, and this entry does not repeat it.
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.