I got the same error due to trying to use an older version Yarn v1 that was installed by default with a stable Node.js 16.x version when I already had Yarn v3 configuration files in the repository, and this was causing conflicts.
I was trying to create a Docker container for an existing repository that already had the following files:
- .yarn/releases/yarn-3.1.1.cjs
- .yarnrc.yml
- .nvmrc
.nvmrc
lts/gallium
This corresponds to Node.js LTS v16.18.0
.yarnrc.yml
nodeLinker: node-modules
plugins:
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"
yarnPath: .yarn/releases/yarn-3.1.1.cjs
So I created a Dockerfile, but when it ran yarn run start
, it returned the same error Usage Error: Couldn't find the node_modules state file - running an install might help (findPackageLocation)
After some investigating I realised that the node:gallium-alpine pre-built Docker image was using Yarn v1 (1.22.19), but the contents of the .yarnrc.yml file indicated that Yarn v3 may be required and was possibly causing a conflict.
So I simply followed these Yarn instructions to install its Corepack and the latest Yarn 3.3.0 and also installed Git since it used that.
I renamed .yarnrc.yml to .yarnrc.yml.old so it wouldn't be used or cause conflicts.
Then I re-generated .yarnrc.yml and the .yarn folder so it would have Yarn 3.3.0 instead of 3.1.1 by running:
yarn policies set-version 3.3.0
But this command only added a line with the new version yarnPath: .yarn/releases/yarn-3.3.0.cjs
to the file, so it was necessary to add the remainder of the .yarnrc.yml.old to the re-generated .yarnrc.yml file too. I could then remove .yarnrc.yml.old
I have included the .dockerignore file too for reference.
Dockerfile
FROM node:gallium-alpine
WORKDIR /app
RUN apk add git
COPY package.json .
COPY .env ./
# copy all except shown in .dockerignore
COPY . .
RUN corepack enable && corepack prepare yarn@stable --activate && yarn set version 3.3.0 && yarn install
CMD ["yarn", "run", "start"]
.dockerignore
.git
node_modules
build
Dockerfile
.dockerignore
.gitignore