201

I'm working with Yarn v0.16.1. If I understand correctly (according to the documentation), yarn global add <package> should be the equivalent of npm install -g <package>. However, when I run the example in the docs (with create-react-app), the command runs successfully but create-react-app is then not available from the command line. Here's the output:

$ yarn global add create-react-app
$ yarn global v0.16.1
[1/4]   Resolving packages...
[2/4]   Fetching packages...
[3/4]   Linking dependencies...
[4/4]   Building fresh packages...
success Installed create-react-app@0.6.0 with binaries:
  - create-react-app
✨  Done in 3.22s.
$ create-react-app --help
-bash: create-react-app: command not found

Doing a global install with npm has the expected result, and everything works. What am I missing with yarn?

Aurora0001
  • 13,139
  • 5
  • 50
  • 53
jaredsk
  • 2,617
  • 2
  • 18
  • 18
  • 'Modern' yarn versions have significantly changed how they handle concerns once handled by global packages. See https://next.yarnpkg.com/cli/dlx, the error message generated when you use `yarn global`, or this answer: https://stackoverflow.com/a/71809403/5440638 – Kay V Apr 09 '22 at 15:58

15 Answers15

310

You should add export PATH="$PATH:$(yarn global bin)" to your ~/.bash_profile or whatever you use. It would solve the issue.

Depending on how you installed it, Yarn's global folder varies for some reason. You can follow this issue here.

Nathan Arthur
  • 8,287
  • 7
  • 55
  • 80
  • 14
    You should also source the ".bash_profile" file after it's been edited to update the changes to your bash shell: `source ~/.bash_profile`. – tim-montague Apr 14 '17 at 12:12
  • 4
    On some systems you may need to run `yarn global bin` yourself and use the literal url in `~/.bash_profile` instead of the subshell, something like this: `export PATH="$PATH:/usr/local/bin"` – Nathan Arthur Aug 20 '17 at 16:28
  • 1
    I had to follow advice from this comment in the issue pointed to in the answer. ```I got global bins working by altering my path from what the docs say in the alternative installation section from export PATH="$PATH:$HOME/.yarn/bin" to export PATH="$PATH:$HOME/.yarn/bin:$HOME/.yarn-config/global/node_modules/.bin"``` https://github.com/yarnpkg/yarn/issues/1321#issuecomment-255532028 – cirovladimir May 03 '18 at 16:09
  • 4
    For Ubuntu users that come across this issue, add `export PATH="$PATH:$HOME/.config/yarn/global/node_modules/.bin"` to your ~/.bash_profile – Pitt Jul 25 '18 at 17:45
  • 4
    for zsh, just put on .zshrc – Shad Aug 10 '19 at 18:52
  • on ubuntu I've added export to `~/.profile` that works too – slavugan Aug 12 '20 at 15:49
  • fis shell: `set -Ua fish_user_paths (yarn global bin)` – Nearoo Dec 03 '20 at 08:21
  • sadly this line adds ugly `warning ../package.json: No license field` to every session – godblessstrawberry Mar 06 '23 at 05:37
114

Update Dec 2018

Just updating the path didn't work for me. I had to also set the yarn prefix.

  • Ubuntu 18.04.1 LTS (bionic)
  • yarn 1.12.3
  • npm v3.5.2
  • node v8.10.0
  • zsh 5.4.2

Steps

  1. Confirm your global bin path

    yarn global bin
    

I got: /home/username/.yarn/bin

  1. set yarn prefix:

    make sure your yarn prefix is the parent directory of your bin directory. You can confirm by running

    yarn config get prefix
    

    when I ran this, my prefix was empty, so I set it:

    yarn config set prefix ~/.yarn
    
  2. add the following to ~/.bash_profile or ~/.bashrc

    export PATH="$PATH:`yarn global bin`"
    

    for zsh users, be sure to add this line to ~/.zshrc

  3. restart your shell or start a new one

    bash -l or zsh

AndrewD
  • 4,924
  • 3
  • 30
  • 32
26

In my case yarn global bin wasn't working.

Try a brew reinstall yarn if that's how you installed yarn

Antoine
  • 1,206
  • 16
  • 19
13

Ran into this issue but on a Windows system. All I had to do was add the yarn global bin to the PATH variable.

setx path "%path%;c:\users\YOURUSERNAME\appdata\local\yarn\bin"

Remember to change YOURUSERNAME to your account username.

You can check where your global bin is by running

yarn global bin

And please don't forget to restart the CMD window that you're working on. Happy coding!

! WARNING !

When executing the command that %path%; in front of your yarn directory is very important. If you don't type it you will definitely replace all your system environment variables.

Option 2

Safer option would be would be to just go to System Properties > Advanced > Environment Variables > select PATH the click Edit > then append and not replace with ;c:\users\YOURUSERNAME\appdata\local\yarn\bin

Thapedict
  • 363
  • 2
  • 9
  • 2
    Resolved my issue on Windows 10 after running 'yarn global add vuepress' and creating the read me, the 'vuepress dev' command was not able to be found prior. – Boz May 29 '20 at 09:52
  • 2
    WARNING: I executed this on PowerShell and lost all my environment variables – now Apr 12 '21 at 11:05
  • 1
    @now you probably didn't include '%PATH%;' before the yarn directory, or used single quotes. Been a victim of this too. Safer option would be to just go to System Properties > Advanced > Environment Variables ..and append that yarn directory – Thapedict Jun 11 '21 at 14:28
10

To reinstall run below.

brew install -g yarn

// Note:(updating homebrew) for Mac users.

brew reinstall yarn

// (if yarn is still not found)

Nick
  • 138,499
  • 22
  • 57
  • 95
heyfranksmile
  • 169
  • 1
  • 5
9

For macOS, you just need one step,

export PATH=~/.yarn/bin:$PATH
Harsh Patel
  • 6,334
  • 10
  • 40
  • 73
4
export PATH="$PATH:$(yarn global bin)"
DEV Tiago França
  • 1,271
  • 9
  • 9
3

Here's the process I used on Mac OSX El Capitan.

  1. Try to install yarn (I used the Curl command) if you have it already. This will bring up if its already installed and will give you the current directory where it exists.

Like this:

curl -o- -L https://yarnpkg.com/install.sh | bash

It will display something like this:

Installing Yarn!
> /Users/{Your Username}/.yarn already exists, possibly from a past Yarn install.
> Remove it (rm -rf /Users/{Your Username}/.yarn) and run this script again.

Do not remove it. Move on to step 2.

  1. Copy the directory listed above. Open your profile. I'm using zsh. So mine was ~/.zshrc. You can find yours and more info here.

  2. Copy the following (replacing your directory and username details as necessary, the one you got from the installation error message).

    alias yarn="/Users/{Your Username}/.yarn/bin/yarn"

  1. Try to run yarn version to check if its working. If it is, you should see a version number displayed in your terminal.

That's it, and what worked for me.

Tapha
  • 1,491
  • 3
  • 17
  • 32
3

AndrewD gave a great answer, but for those on Windows it's a bit different, especially step 3.

Please do not use the command setx as @Thapedict said. It will remove all your previous paths you had and replace it with only the given directory. If you want to use setx, I think there might be a flag to make it append the existing path variables, but I don't know which one. Using only setx will not append to it.

I would just do System Properties > Advanced > Environment Variables > Edit Path > New > c:\users\YOURUSERNAME\appdata\local\yarn\bin

to make sure you don't remove your previous variables.

Luddebror
  • 31
  • 1
  • 3
  • adding `%PATH%;` before the directory you want to include is the way you append it. If you omit %PATH%; it will most definitely replace instead of append. And wrapping with double apostrophe is also as important. https://www.windows-commandline.com/set-path-command-line/ – Thapedict Jun 11 '21 at 14:06
2

On Ubuntu, I fixed by installing the package like this:

sudo yarn global add <package> --prefix /usr/local
Florian Ludewig
  • 4,338
  • 11
  • 71
  • 137
2

To fix this issue in 2023:

corepack enable

I started running into this issue out of all of a sudden in July 2023 across multiple computers. I tried all different kinds of updates to my bash/zsh profiles - nothing worked except running corepack enable manually. More info on corepack here.

adstwlearn
  • 720
  • 7
  • 9
1

When switching from bash to zsh, I got the same error, it worked for me:

in your .zshrc 

# NVM Stuff
export NVM_DIR="$HOME/.nvm"
. "$(brew --prefix nvm)/nvm.sh"

# YVM Stuff
export YVM_DIR="$HOME/.yvm"
. "$(brew --prefix yvm)/yvm.sh"

As I understand it, my mistake arose due to the lack of access to version managers

ex btrg
  • 11
  • 4
1

I faced a similar issue on Windows after installing Yarn then Vue Cli. Packages were installed but not accessible. The problem was that the directory where yarn packages are installed is not in PATH.

The default installation directory for Yarn packages in my case was C:\Users\USERNAME\AppData\Local\Yarn

So make sure to add this directory to path.

Denmau
  • 231
  • 2
  • 5
1

It's no longer necessary to modify your path for yarn

Newer versions of yarn include yarn create and yarn exec

In the case of create-react-app you can run yarn create react-app see this blog post https://classic.yarnpkg.com/blog/2017/05/12/introducing-yarn/

To run a non-creating package like serve, you can still run it without modifying your path! After installing it with yarn global add serve try this: yarn exec serve

rob
  • 13
  • 5
0

Putting this answer so that hopefully it can come in google and help others.

expo command not found after using yarn global add expo-cli don't work in ubuntu 20.04

Neel
  • 428
  • 1
  • 4
  • 12