11

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

Arman Ebrahimi
  • 2,035
  • 2
  • 7
  • 20

5 Answers5

10

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 –

Tasos
  • 1,880
  • 13
  • 19
  • Thanks. But I did it already. So installed dependencies. I updated the question. – Arman Ebrahimi May 10 '22 at 12:53
  • Can you try deleting the `node_modules` folder and running yarn install && yarn start again? – Tasos May 11 '22 at 11:12
  • Thanks again. Only `node_modules`? (how about `.yarn or yarn.lock?`). I deleted it now, but it gives the same error again. – Arman Ebrahimi May 12 '22 at 05:07
  • 1
    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: https://stackoverflow.com/questions/69692842/error-message-error0308010cdigital-envelope-routinesunsupported Let me know how that goes – Tasos May 12 '22 at 10:07
  • Done, happy to help @ArmanEbrahimi – Tasos May 16 '22 at 11:55
  • 1
    @Tasos Please bolden the sentence _before running any npm script_ as this is true **even if** the npm script **is** `yarn install`, as in npm script: `"ci": "yarn install"`. – Moshisho Jun 08 '23 at 10:44
  • 1
    @Moshisho done! – Tasos Jun 09 '23 at 09:18
4

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
Luke Schoen
  • 4,129
  • 2
  • 27
  • 25
1

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

thedanotto
  • 6,895
  • 5
  • 45
  • 43
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
Michael Fry
  • 1,090
  • 9
  • 12
0

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

Curious_guy
  • 161
  • 11