yarn command not found after installing via npm
Asked Answered
W

11

31

As per the yarn installation for yarn v2, they want you to install using npm install -g yarn. So I ran sudo npm install -g yarn on Ubuntu 20.04. But after I do that, it says command not found.

❯ sudo npm install -g yarn

> [email protected] preinstall /usr/local/lib/node_modules/yarn
> :; (node ./preinstall.js > /dev/null 2>&1 || true)


❯ yarn --version
zsh: command not found: yarn
Weft answered 27/1, 2021 at 19:33 Comment(2)
Run it without sudo. If you have your paths set up correctly then this issue is because of the users.Concentrate
sometime you just miss the type of terminal your using zsh, bash etch.Heelpost
G
45
sudo npm install -g npm

then

sudo npm install -g yarn

Then reboot your system. That did it for me. Before a reboot only sudo yarn worked. I tried looking at file permissions but everything seemed in order and the files were executable as expected. Nevertheless after a reboot it worked.

If you go to /usr/local/bin after the installation there's a link there to where yarn.js lives, as expected, and file permissions for it were also correct.

/usr/local/bin is added to $PATH, so it's surprising that it doesn't see the new cmd right away, but perhaps it didn't reload or map it until after the reboot? I don't know. But I just spent a good hour trying to figure this out so I'm posting what worked for me to spare other the hassle.

Goraud answered 27/2, 2021 at 10:43 Comment(4)
For future viewers, I deleted npm and npm_cache located in appdata/roaming prior to doing this as even this fix didn't solve the issue for me. After deleting, I ran these commands, restarted my pc, and voila, problem solved.Statistical
With this on Ubuntu 20.04 no reboot was required.Disepalous
"npm install -g yarn" worked for me with no sudo and no restartHowe
thank you! this worked for me no restartBeatty
F
14

TL;DR If you are managing node via nvm, then probably the path to yarn binary is not included in the $PATH variable. You should add this -

# Add this at the end (or after the $NVM_DIR initialization)
#   in your profile - .bashrc | .zshrc | .profile, etc
export PATH="`yarn global bin`:$PATH"

at the end of your profile file (.zshrc for me) or at least after the $NVM_DIR initialization.


I have recently faced this issue and while searching for a solution, I landed up here.

Here is what my environment looks like:

  • OS: Ubuntu 20.04
  • Shell: zsh
  • NodeJS: managing it via nvm, and NOT apt.

After going through all the answers, I was not keen on uninstalling anything. So I tried to dig a bit deeper.

I installed yarn via npm install -g yarn command. So the first thing I wanted to verify was the location of the yarn binary. To do this, I ran the command where yarn which lists the installation path for the yarn binary.

$ where yarn

/home/<user_name>/.nvm/versions/node/v16.11.1/bin/yarn

Then it hit me. In my .zshrc file, I had added the yarn global bin command (which spills out the directory of all the global packages installed by yarn) at the top like so:

# Top of my .zshrc file
export PATH="`yarn global bin`:$HOME/bin:/usr/local/bin:$PATH"

and as per the installation instruction of nvm, the $NVM_DIR (the variable which holds the nvm directory path) was added at the end of my .zshrc file.

So when I was starting up my shell, it was actually trying to load the yarn command (present inside the nvm directory) even before loading the $NVM_DIR path.

To solve this, I tweaked my .zshrc file and moved the yarn global bin command after the $NVM_DIR like this:

# Top of my .zshrc file
export PATH="$HOME/bin:/usr/local/bin:$PATH"

# ...
#
# Something in between
#
# ...

# Bottom of my .zshrc file
export NVM_DIR="${HOME}/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion

# Here is where I have added the path to yarn global
export PATH="`yarn global bin`:$$PATH"

I hope that this would be of help.

Feminacy answered 21/10, 2021 at 4:39 Comment(3)
For me checking nvm current showed "none". After selecting a dedicated version, e.g. nvm use 16 or 'lts' it worked again.Sauncho
Does it work to run yarn inside the rc startup file if yarn isn't on the path?Tetrachord
Moving the NVM initialization above the yarn export did the trick!Ailurophobe
A
13

This solved it for me:

corepack enable

(if you get "Internal Error: EACCES: permission denied", run it with sudo)

This is also recommended by the Yarn documentation: https://yarnpkg.com/getting-started/install

Additament answered 26/2, 2022 at 16:9 Comment(1)
After installing Cypress in my project, and building with yarn, zsh did not recognize yarn, cypress or other commands. This was the only solution that worked. Thank you!Unipod
W
5

Uninstall cmdtest:

sudo apt remove cmdtest

Then, run these commands:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install yarn
Wellworn answered 26/7, 2021 at 3:24 Comment(0)
D
2

If you want to avoid reboot, use /usr/local/lib/node_modules/yarn/bin/yarn --version

Demetriusdemeyer answered 22/7, 2021 at 15:33 Comment(0)
S
1

The yarn documentation is missing a step, you need to restart your computer between this installation and running yarn --version.

This worked for me

Sellingplater answered 15/12, 2021 at 8:25 Comment(0)
R
1

This is different from the problem OP had but you also get this error if you are working in an environment with multiple node versions. In this case, you need to have yarn installed under each version of node, like so

/node/vx.x.x/lib/node_modules/yarn/bin/yarn.js 

After switching versions, if the above is not present then just run

npm install -g yarn 

If yarn is installed under the version than the other answers will address the problem.

Retroact answered 2/3, 2023 at 20:11 Comment(0)
P
0

I recently had a similar situation and here is how I solved it.

First I troubleshoot the current npm installation:

npm config -list

I had a ~/.npmrc file that had a different prefix:

PREFIX=/opt/homebrew

That made my npm installation look for globally installed packages under /opt/homebrew.

In my case, I'm using a different npm installation (not with homebrew anymore). A simple fix is to remove this custom PREFIX from the ~/.npmrc file and the problem was solved.

Now npm looks for globally installed packages under /usr/local/bin/.

Privative answered 13/2, 2022 at 15:27 Comment(1)
The command probably should read npm config list. With npm config -list an error is reported.Soble
E
0

If you install yarn with sudo, it will only be available for the super user. So you can install it without prefixing the command with sudo and it will work fine.

npm -i g yarn

Experienced answered 13/7, 2023 at 19:28 Comment(0)
O
-1

It means it's unable to find yarn.

To fix it do following steps

  1. Know yarn path
where yarn
  1. Update the path variables to include the path given by step 1.

In case of Mac, open .zshrc from your user directory and add following into it.

export YARN_PATH=<path_mentioned_in_step_1>
export PATH=$PATH:$YARN_PATH

In case of Windows, YARN PATH should be added in environment variable called Path

  1. If you are getting this issue on VS Code terminal, Close and open the VS Code and run the command. If you are getting this issue on terminal, close and open the terminal and run the command.
Overprize answered 31/10, 2023 at 4:43 Comment(0)
C
-2

I installed yarn with npm install -g yarn on git bash and I tested it with yarn -v that show the version of the installed yarn, but when I used yarn start it gives me this error

C:\Users\{username}\AppData\Roaming\npm/node_modules/node/bin/node: line 1: This: command not found

These are simple steps that I used to fix my problem on Windows 10:

  1. Uninstall node.js
  2. Restart your computer
  3. Delete your C:\Program Files\nodejs and C:\Users\{username}\AppData\Roaming\npm
  4. Install node.js again and check it with node -v
  5. Start your vs code as an admin and write npm install
  6. Write yarn start
Conspiracy answered 9/2, 2022 at 16:36 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Deposal

© 2022 - 2024 — McMap. All rights reserved.