Executive Summary & Root Cause Analysis: The error
yarn: command not found(or'yarn' is not recognized as an internal or external commandon Windows) occurs when your terminal shell cannot locate theyarnexecutable in any directory listed in your systemPATH. Common root causes include: (1) Node.js 16.10+ and modern LTS versions bundle Corepack for Yarn management, but Corepack is disabled by default until explicitly enabled, (2) switching Node.js versions vianvmorfnmwithout reinstalling global packages or re-enabling Corepack for the new Node binary, and (3) a missing Yarn global binary directory in your shell configuration (~/.zshrc,~/.bashrc, or Windows Environment Variables).
Step 1: Quick Fix – Enable Corepack (Recommended for Node.js 16.10+)
The official and fastest way to activate Yarn in modern Node.js environments (v16.10, v18, v20, v22+) is through Corepack, which ships built into Node.js:
# 1. Verify Node.js is installed and accessible
node -v
# 2. Enable Corepack to expose the yarn shim
corepack enable
# 3. Prepare and activate the latest stable Yarn release
corepack prepare yarn@stable --activate
# 4. Reload your shell or check the active Yarn version
yarn --version
If corepack itself is missing (for example, in stripped Docker images or older Node.js builds), install Yarn globally via npm:
npm install --global yarn
Step 2: Fix Shell PATH and NVM Version Switching Issues
If you previously installed Yarn via npm or Homebrew, yet your terminal still fails to find it after a reboot or Node version switch, your shell PATH is missing the global bin directory.
- On macOS / Linux (Bash or Zsh): Find npm’s global bin directory and export it in your shell profile:
# Locate your npm global prefix npm config get prefix # Add the bin directory to ~/.zshrc (macOS) or ~/.bashrc (Linux) echo 'export PATH="$(npm config get prefix)/bin:$PATH"' >> ~/.zshrc echo 'export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"' >> ~/.zshrc # Reload your shell profile immediately source ~/.zshrc - When using NVM (Node Version Manager): Global packages and Corepack shims are isolated per Node.js version. Whenever you run
nvm install <version>ornvm use <version>, runcorepack enableagain for that active version. - On Windows (PowerShell / CMD): Check your npm global path via
npm config get prefix(commonly%AppData%\npm), add it to System Properties > Environment Variables > Path, and restart your terminal.
Step 3: Alternative Workarounds – Homebrew, Winget, and CI/CD Pipelines
If Corepack conflicts with an existing global binary or you are setting up an automated CI/CD container:
- Install via OS package managers (macOS / Windows):
# macOS via Homebrew (note: installs Node if not present) brew install yarn # Windows via Winget or Chocolatey winget install Yarn.Yarn - Inside Dockerfiles or CI/CD runners (GitHub Actions / GitLab CI):
# Explicitly enable Corepack in Dockerfile before running yarn install RUN corepack enable && corepack prepare yarn@stable --activate RUN yarn install --immutable
Verification & Testing Steps
Confirm that your terminal resolves the exact yarn binary path cleanly and can execute package management commands:
# Confirm the resolved executable path (macOS/Linux)
which yarn
# On Windows PowerShell:
# Get-Command yarn
# Verify installed Yarn version
yarn --version
# Test inside a project directory
yarn install --check-files
Summary Comparison Table
| Resolution Method | Operating System | Persistence | Best Used For |
|---|---|---|---|
corepack enable |
macOS, Linux, Windows | Per Node.js installation | Modern Node.js (v16.10+) & Yarn Berry projects |
npm install -g yarn |
macOS, Linux, Windows | Per Node/NVM version | Older Node versions or environments without Corepack |
Update PATH in ~/.zshrc / Env Vars |
macOS, Linux, Windows | Permanent across terminals | Fixing installed Yarn missing from shell lookup |
brew install yarn / winget |
macOS / Windows | System-wide | Decoupling Yarn from NVM version switches |
Leave a Reply