History#
Previously, the package management tool Yarn
was installed using various methods, such as executing npm install -g yarn
.
Once installed, the yarn CLI could always be used globally in the terminal.
Now#
With the development of the times, the installation method of Yarn has also changed. The official website states that the preferred way to manage Yarn is by-project and through Corepack, because "just as the versions of project dependencies are locked, the package manager itself should also be locked."
The benefit of using Corepack
is that it allows for faster updates to the CLI.
Enable#
Starting from Node.js v16.9.0 and v14.19.0, Corepack is included. However, this feature is currently in experimental mode and is disabled by default, so you need to execute corepack enable
to enable it, after which you can use the yarn
command.
Set Download Source#
Since Corepack defaults to downloading yarn
or pnpm
from https://registry.npmjs.org, if there is no magic, it will lead to the following error during download:
Error: Error when performing the request to https://registry.npmjs.org/pnpm/latest; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting
Therefore, before installation, execute the following command to set a temporary environment variable in the current terminal (which will be invalid after the window is closed):
set COREPACK_NPM_REGISTRY=https://registry.npmmirror.com
After setting the variable, execute yarn install
or pnpm install
to install.
Check#
Execute yarn exec env
. If you get a string of path output, it means Corepack has been correctly installed, which can quickly check if Corepack is enabled. If not, please refer to Troubleshooting for solutions.
Update#
Whenever you want to update Yarn to the latest version, simply run:
yarn set version stable
yarn install
Then, Yarn will configure your project to use the latest stable binary.