I installed yarn globally, but got error "zsh:command not found : yarn"

Viewed 3357
npm install -g yarn

> yarn@1.22.15 preinstall /Users/myname/.npm-global/lib/node_modules/yarn
> :; (node ./preinstall.js > /dev/null 2>&1 || true)

/Users/myname/.npm-global/bin/yarn -> /Users/myname/.npm-global/lib/node_modules/yarn/bin/yarn.js
    /Users/myname/.npm-global/bin/yarnpkg -> /Users/myname/.npm-global/lib/node_modules/yarn/bin/yarn.js
    + yarn@1.22.15

As I use a mac, I opened the zshrc file. and then write the code below

export PATH=$PATH:/Users/myname/.npm-global/lib/node_modules/yarn

However, when I entered the yarn --v command, I got the same error as the title.

Could you please tell me what the problem is? What should I do when I have installed a variable globally?

2 Answers

Add the global npm bin to PATH instead:

export PATH="$PATH:$(npm bin -g)"

In your case, this is the same as:

export PATH="$PATH:/Users/myname/.npm-global/bin"

Here is what worked for me on the Mac OS Monterey 12.3.1

npm install -g yarn
npm config get prefix

My command output was: /Users/my_username/.npm-packages

Append that in your /etc/paths using nano

Open up Terminal.

Run the following command:

sudo nano /etc/paths

Enter your password, when prompted.

Go to the bottom of the file, and enter the path you wish to add.

Hit control-x to quit.

Enter “Y” to save the modified buffer.

That’s it! To test it, in new terminal window, type:

echo $PATH

You will see /Users/my_username/.npm-packages/bin appended to your path

Yarn should now work

yarn --version

You will see 2.4.x

Related