For MacOS users, Homebrew is a convenient package manager for installing libraries and developer tools. However, when installing Yarn through Homebrew, you may encounter an issue where the Yarn version cannot be changed globally. This problem arises when older projects run on a stable version (e.g., 1.xx) and newer projects use different Node.js versions (e.g., v16, v10).
The root cause of this issue is that Yarn depends on the Node.js package. To resolve this, you need to install Yarn using npm install -g yarn instead of installing it directly with Homebrew. Additionally, you should use the system Node.js installation to avoid conflicts with any necessary global checks.
Here are the steps to resolve the issue:
- Install the system Node.js version using Homebrew:
brew install node
- Install the default Yarn version when using the system Node.js:
note: we need to have a yarn version globally to avoid some unexpected errors like husky
npm install -g yarn@berry #you can change `berry` to any version
- Install Node Version Manager (nvm) to manage different Node.js versions:
brew install nvm
- Install the desired Node.js versions using nvm:
nvm install v16 #you can change v16 to any version of node
- Use a specific Node.js version and install the corresponding Yarn version:
nvm use v16 && npm install -g yarn@berry # you can change v16 to any node version
By following these steps, you can ensure that each project uses the appropriate Yarn version based on the Node.js version installed through nvm. This approach allows you to work seamlessly with different Node.js and Yarn versions across multiple projects.
Top comments (0)