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

npm install -g yarn

> [email protected] 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
    + [email protected]

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?



Solution 1:[1]

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"

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fn control option
Solution 2 Carlos Ferreira