13

I'm trying to install a package using yarn, but I keep getting 'Cannot find module' error. It gives this error when I try to run any command using yarn in this folder. Notably, the error started occuring after I deleted the .yarn folder.

node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module '/home/chronos/user/Downloads/codes/some-react-app/.yarn/releases/yarn-3.1.1.cjs'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Should I delete my yarn.lock file, or what can I do to solve the problem.

scoochy
  • 693
  • 1
  • 6
  • 14

4 Answers4

22

UPDATE:

It seems that the link I provided earlier has been removed. It was just the yarn-x.x.x.cjs file that was there. Another solution I found was to delete ALL the global yarn configuration files. On bash terminal that's rm -f ~/.yarn*. Run yarn set version stable or your specific version, then run yarn install again and you should be good to go

Old post:

I found a fix.

For those who face this problem, what I did was, from my project directory, create a directory .yarn and inside it another directory, releases, then download: https://raw.githubusercontent.com/GlobexDesignsInc/gxdrecords.com/master/.yarn/releases/yarn-3.1.1.cjs and paste in the releases folder.

You should now be able to run yarn commands from your project directory.

scoochy
  • 693
  • 1
  • 6
  • 14
  • 2
    Seems like mentioned link is broken – Prakhar Nigam Mar 25 '22 at 07:28
  • 1
    dont run rm -f ~/.yarn* . it deletes the global configuration. not recommended. if you must. you can remove just .yarnrc in current project as well as .yarn folder. then in your package.yml , remove packageManager line which points to yarn. from there just run yarn again, set the version if you wish and it will recreate. – mirageglobe Jan 17 '23 at 18:39
  • 1
    @mirageglobe Deleting global configuration was what I needed, since yarn was misconfigured on my machine. In fact, changing repo configuration is not possible for me at work. – Zach Apr 03 '23 at 22:41
6

Running rm ~/.yarnrc and rm ~/.yarnrc.yml and worked for me. Don't worry if either file isn't there. ~/.yarnrc was not found in my case so I ended up deleting ~/.yarnrc.yml only. I logged out and in to the terminal and ran yarn --version and got 1.22.17

4

just to add to above answers. there are two possibilities of encountering this, either you have run yarn (with version 3+) in your global settings ~\ or it might be the project you are working on ~/myfavproject/* where yarn was previously initated.

a few steps which you can use in your project folder,

  • remove node_modules rm -fr node_modules
  • remove .yarn folder which has various *.cjs yarn releases rm -fr .yarn
  • edit your package.json and check/remove the line for packageManager: xxxxx see below
  },
"packageManager": "yarn@3.3.1"
}

after,

  • check your yarn version yarn --version
  • set your yarn version yarn set version berry or yarn set version stable if you wish (https://yarnpkg.com/cli/set/version)
  • run yarn again yarn which will add the files and also update package.json
mirageglobe
  • 2,446
  • 2
  • 24
  • 30
3

I set my yarn version (in yarnrc.yml) to the default yarn (I used homebrew) yarnPath: /usr/local/bin/yarn

Then, I was able to rerun yarn set version berry

Then reset yarnPath to .yarn/releases/...

codster
  • 71
  • 4