使用yarn global命令报错:找不到'global'文件或目录求助
Hey there, let's work through why you're hitting that ERROR: [Errno 2] No such file or directory: 'global' when running yarn global add yo (or any other yarn global command). Here are the most common fixes to test out:
1. Check your Yarn version first
The global command was removed starting with Yarn 2.x (also called Yarn Berry). If you're running a newer Yarn release, this is exactly why you're seeing the error.
- Run this to check your version:
yarn --version - If the output is
2.xor higher, you have two solid options:- Option A: Use Yarn's modern alternatives
To run a package temporarily without global installation, useyarn dlx:
If you really need a persistent global install, switch to npm for this task:yarn dlx yonpm install -g yo - Option B: Downgrade to Yarn Classic (1.x)
If you prefer sticking with theyarn globalworkflow, revert to the classic version:npm install -g yarn@classic
- Option A: Use Yarn's modern alternatives
2. Verify your global Yarn installation (for Yarn Classic)
If you're already on Yarn 1.x but still getting the error, there might be an issue with how Yarn is installed globally:
- First, confirm you're running the global Yarn executable (not a local project-specific one):
- On macOS/Linux:
The output should point to a path likewhich yarn~/.yarn/bin/yarnor/usr/local/bin/yarn. - On Windows:
Look for a path in your system'swhere yarnProgram FilesorAppDatadirectory.
- On macOS/Linux:
- If the path points to a local project's
node_modules/.bin/yarn, either run commands with the full path to your global Yarn, or ensure your global Yarn'sbindirectory is in your system'sPATH. - If the installation seems corrupted, reinstall Yarn Classic:
npm uninstall -g yarn npm install -g yarn@classic
3. Check your system's PATH environment variable
Sometimes the error pops up because Yarn's global bin directory isn't in your PATH, so your shell can't resolve the global command context:
- On macOS/Linux:
- Check if
~/.yarn/binis in your PATH:echo $PATH - If it's missing, add it to your shell config file (like
~/.bashrc,~/.zshrc):export PATH="$HOME/.yarn/bin:$PATH" - Reload the config file to apply changes:
source ~/.zshrc # or source ~/.bashrc depending on your shell
- Check if
- On Windows:
- Open System Properties → Advanced → Environment Variables.
- Under "System Variables", find the
PATHentry and edit it. - Add the path to Yarn's global bin directory (usually
C:\Users\YourUsername\AppData\Roaming\Yarn\bin). - Restart your command prompt or PowerShell for changes to take effect.
After trying these steps, run yarn global add yo again—it should work as expected!
内容的提问来源于stack exchange,提问作者smaug




