Why do I get this error: Couldn't find the node_modules state file - running an install might help (

I tried a lot, but my projects don't run with yarn, while already worked(made with create-react-app). I tried: yarn install - npm install --global yarn - npm update --global yarn - yarn version apply and ect.

I tried a lot, but my projects don't run with yarn, while already worked(made with create-react-app).

I tried: yarn install - npm install --global yarn - npm update --global yarn - yarn version apply and ect.

when I run yarn start then:

enter image description here

Edit1: When I create a new project(with create-react-app), not occur any error. It gives an error only for the previous projects.

I already did yarn install for those directories, and yarn start worked. So run yarn install doesn't fix that. Although when I run run install again, it gives this error:

enter image description here

Edit2: When I remove node_modules and .yarn and yarn.lock, then run yarn install and thenyarn start, it gives another error:

enter image description here

5 Answers

You should run

yarn install 

before running any npm script (yarn start). It works when you use create-react-app because it installs dependencies by itself.

About the digital envelope error, You get this error because of the NodeJS version (most likely). If you use the latest LTS downgrade to the previous LTS version. You can read more here: Error message "error:0308010C:digital envelope routines::unsupported" Let me know how that goes –

7

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 

I struggled with this error for a LONG time in my ci/cd pipeline.

yarn install was failing to properly complete, but it exited with a 0 (success). Since it failed to properly install, it couldn't find the state file.

TLDR: ensure yarn install properly completes

0

I had accidentally ran yarn in a repo that was setup to use npm, which silently mutated my global yarnrc file, causing this error.

My solution was updating the yarnrc file in the repo to contain the entry:

nodeLinker: pnp 
1

Running yarn install before running any command to start the project did the trick for me . I still do not know how this happened

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobmtpZmaGc4WOsJ%2ByZZSkeqp5xp6rZqyYnsBusdGrpqtlk6TCrbDNrWSfoZ6ZerW0xGalqJyVYrqwsNSlnKxlo6mutbGMn6ClnV2nwq%2B6yKeeZpme

ncG1vNJzZmiom6HAb7%2FFqGpnnJmctrWty6ianpmeqL2ir8SsZZynnWTEqcWMnaZmoV2csrV506GgrGWVp7%2Bwvoycpq6klKN6tXnFoqWdZaSdsm66zp2cZqWfmcKtsdJmqq2ZpJp6p7XLnmSrrZ6jtq%2BzjJqlZqGeqMGiuMtmpKKfmKl6qbHLqWWhrJ2h

 Share!