5 Quick Fixes for 'Zsh Command Not Found Yarn' Error

If you’re encountering the 'zsh: command not found: yarn'
error, it’s likely because your system doesn’t recognize the yarn
command. This issue often arises when Node.js and Yarn are installed, but the system’s shell (Zsh in this case) can’t locate the yarn
executable. Below are 5 quick fixes to resolve this error, tailored for developers using Zsh on macOS, Linux, or other Unix-like systems.
1. Verify Yarn Installation
Before troubleshooting, ensure Yarn is actually installed. Run:
npm list -g yarn
If Yarn is installed globally, you’ll see its version number. If not, install it using:
npm install -g yarn
2. Update Your Shell’s PATH Variable
The PATH
environment variable tells your shell where to look for executables. If Yarn’s installation directory isn’t in PATH
, Zsh won’t find it.
Step-by-Step Fix:
Locate Yarn’s installation path. Typically, it’s in:
- macOS/Linux:
/usr/local/bin
or~/.npm-global/bin
- Windows (WSL/Git Bash):
/usr/bin
or/usr/local/bin
- macOS/Linux:
Add the path to your shell’s configuration file (
~/.zshrc
for Zsh):echo 'export PATH="$PATH:/path/to/yarn/bin"' >> ~/.zshrc
Replace
/path/to/yarn/bin
with the actual path (e.g.,/usr/local/bin
).Reload the shell configuration:
source ~/.zshrc
3. Use npx
to Run Yarn Temporarily
If you don’t want to modify PATH
, use npx
to execute Yarn commands directly:
npx yarn <command>
For example:
npx yarn install
This works because npx
searches for the executable in local and global npm directories.
4. Reinstall Yarn Globally with the Correct Permissions
Sometimes, Yarn is installed in a location that requires elevated permissions. Reinstall it globally with sudo
:
sudo npm install -g yarn
Caution: Using sudo
with npm can lead to permission issues. Instead, consider using a Node.js version manager like nvm to manage global installations.
5. Check for Conflicting Yarn Installations
Multiple Yarn installations can cause conflicts. Verify installed versions:
which -a yarn
If multiple paths appear, remove the conflicting installation or update your PATH
to prioritize the correct one.
Bonus: Use a Node.js Version Manager (Recommended)
Tools like nvm or fnm simplify Node.js and package management. They automatically handle PATH
updates and avoid global installation issues.
Install nvm and Yarn:
- Install nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
- Install Node.js and Yarn:
nvm install --lts npm install -g yarn
- Restart your terminal and test
yarn
.
FAQ Section
Why does Zsh say 'command not found' even after installing Yarn?
+This happens when Yarn’s installation directory isn’t in your shell’s PATH
. Update ~/.zshrc
with the correct path and run source ~/.zshrc
.
Can I use Yarn without fixing the PATH issue?
+Yes, use npx yarn
to run Yarn commands temporarily without modifying PATH
.
Should I use `sudo` to install Yarn globally?
+Avoid using sudo
with npm. Instead, use a Node.js version manager like nvm
to handle global installations safely.
How do I check if Yarn is installed correctly?
+Run yarn --version
. If it returns a version number, Yarn is installed and accessible.
What if I still get the error after trying all fixes?
+Ensure your shell configuration files (~/.zshrc
, ~/.bashrc
) are correctly sourced. Restart your terminal and verify the PATH
variable with echo $PATH
.
By following these steps, you’ll resolve the 'zsh: command not found: yarn'
error and get back to coding seamlessly. For long-term stability, consider adopting a Node.js version manager to avoid similar issues in the future.