zsh: command not found: yarn
Published ยท Updated
zsh: command not found: yarn means zsh searched every directory in your
PATH and none of them holds a yarn executable. Yarn is not part of
Node.js, so installing Node does not give you yarn. On a Mac the cause is
almost always one of three things:
- Yarn was never installed for the Node.js you are running now.
- The project uses modern Yarn through Corepack, and Corepack is not enabled.
- Yarn is installed, but its directory is not on the
PATHof this shell.
Find out which case you are in
Run these in the terminal that shows the error:
command -v node npm corepack yarn
node --version
npm prefix -g
- If
nodeis also missing, fix Node.js first; see node: command not found. npm prefix -gprints where global npm packages go. Their commands live in thebinfolder under it:/opt/homebrew/binfor Homebrew Node on Apple silicon,/usr/local/binfor Homebrew on Intel or the nodejs.org installer, and~/.nvm/versions/node/<version>/binwith nvm.- Look at the project's
package.json. A"packageManager": "[email protected]"line means the project expects Corepack (case 2). No such line, or ayarn.lockwith a# yarn lockfile v1header, means Yarn Classic 1.x.
Fix 1: enable Corepack (Yarn 2 and later)
Corepack reads packageManager and runs the exact Yarn version the project
asks for. Node.js ships it from 14.19 and 16.9 up to 24. Node.js 25 and
later do not, so install it from npm first:
node --version # v25 or later? then:
npm install -g corepack
corepack enable # creates the yarn and pnpm shims
rehash
yarn --version
corepack enable writes the yarn shim next to the corepack binary. With
Homebrew Node or nvm that is the same folder as node, which is already on
your PATH. If you install Corepack
with Homebrew instead (brew install corepack), note that it conflicts with
Homebrew's yarn formula; use one or the other.
Fix 2: install Yarn Classic (1.x)
For an older project that is still on Yarn 1, either of these works:
npm install -g yarn # installs into the bin folder of `npm prefix -g`
# or
brew install yarn # Homebrew's yarn formula is Yarn 1.22
Do not use sudo npm install -g. If npm says it has no permission to write
to the global folder, your Node install is owned by root; switch to Homebrew
Node or nvm instead of forcing it with sudo.
Fix 3: yarn exists but zsh cannot see it
Find it, then add its folder to PATH:
ls "$(npm prefix -g)/bin/yarn" /opt/homebrew/bin/yarn /usr/local/bin/yarn 2>/dev/null
print -l $path # the directories zsh searches, one per line
If yarn sits in a folder that is missing from the list, add that folder
in ~/.zprofile and open a new terminal tab:
# ~/.zprofile on Apple silicon with Homebrew
eval "$(/opt/homebrew/bin/brew shellenv)"
For a custom npm prefix, add its bin folder instead, for example
export PATH="$HOME/.npm-global/bin:$PATH". Why ~/.zprofile and not
~/.zshrc, and what the shell reads in which order, is in
zsh: command not found on macOS: how PATH works.
It worked yesterday: nvm and version switches
With nvm, each Node.js version has its own global packages. yarn installed
under Node 20 is gone after nvm use 22. Either install it again for the new
version, carry your global packages over when you install a version, or let
Corepack handle Yarn per project:
nvm install 22 --reinstall-packages-from=20
The same thing happens in a new terminal if your ~/.zshrc loads nvm but the
default alias points at a version that does not have Yarn. Check with
nvm alias default.
yarn works in Terminal but not in an editor or script
Terminal.app, iTerm2 and the VS Code terminal start zsh as a login shell and
read both ~/.zprofile and ~/.zshrc. A script run by cron, a Git hook or
a GUI app does not read them. In those places call Yarn by its full path from
command -v yarn, or set PATH in the tool's own configuration.
The other error: yarn global commands not found
If yarn itself works but a tool you installed with yarn global add
does not, that is a different folder. Yarn Classic puts global binaries in
the directory printed by yarn global bin; add that one to PATH. Modern
Yarn has no global installs: use yarn dlx <package> instead.
Sources
related.
Ruslan
About the author