$ yarn install leads to Cannot find module yarn-berry.js

Viewed 9905

New to react and am having trouble with the yarn install command where i get the following error:

Error: Cannot find module '/Users/MacPro/Desktop/Developer/React/.yarn/releases/yarn-berry.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1029:15)
at Function.Module._load (internal/modules/cjs/loader.js:898:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47   code:'MODULE_NOT_FOUND',  requireStack: []

Anything related to the yarn function returns this error. Have tried uninstalling and re-installing through homebrew but always results in this. npm install -g yarn says it is updated and installed as well.

Any help?

Also, it worked fine a few weeks ago.

Thank you in advance.

3 Answers

Ok so I guess like me you did not read the whole Yarn documentation. To get out of this:

  1. Remove the .yarnrc.yml file in your project
  2. Configure again Yarn 2: yarn set version berry
  3. Configure your .gitignore file according to Yarn documentation: https://yarnpkg.com/getting-started/qa#which-files-should-be-gitignored

If like me, you do not want to commit 1000+ dependency files to your repository, add these lines to your .gitignore file:

.yarn/*
!.yarn/patches
!.yarn/releases
!.yarn/plugins
!.yarn/sdks
!.yarn/versions
.pnp.*

After that, your commit should contain:

  • the updated .yarnrc.yml file (or none if the file did not change)
  • the .yarn/releases/yarn-berry.js file (maybe it is named otherwise like yarn-berry.cjs, the important thing is that this file matches what is contained in your .yarnrc.yml file)

I think that you tried to update your version of Yarn to version 2 by vaguely following (as I did) the tutorial here :https://yarnpkg.com/getting-started/migration

Maybe you realised you did not like Yarn 2 (no node_modules folder anymore... seriously Yarn?), uninstalled it, deleted different files manually, and now end up with this error (at least it was my case).

You are now trying to make a clean install of Yarn 1 because you still prefer it to npm (as I do) and you end up getting those crazy errors telling Yarn is looking form something mentioning some Berry thing you can't find, this error :

   node:internal/modules/cjs/loader:928
          throw err;
          ^
        
        Error: Cannot find module '/Users/Username/.yarn/releases/yarn-berry.cjs'

You have to first make a complete uninstall of Yarn. Based on what you used to install it :

npm uninstall -g yarn 

or

brew uninstall yarn

Then, and this is the most important part, go to your Home folder (on Mac Only) and list the hidden folders with

ls -al

You will have two elements mentioning yarn :
.yarnrc.yml and .yarnrc

Type the following to remove them:

rm -rf .yarnrc

and

rm -rf .yarnrc.yml

Once done, go and make a brand new install of Yarn using npm or brew :

npm install - g yarn

or

brew install yarn

That should be it.

RU sure you have yarn installed globally? Try npm install yarn -g. If you are a Windows user just download yarn from its website named .msi file.

Related