重要提示: 此中文文档针对的是 Yarn 的最新版本。
有关 1.x 版本的中文文档,请点击进入 classic.yarnpkg.cn。
Yarn
简介安装用法Editor SDKsMigrationQuestions & AnswersRecipes

安装

Edit this page on GitHub

安装 Corepack

The preferred way to manage Yarn is through Corepack, a new binary shipped with all Node.js releases starting from 16.10. It acts as an intermediary between you and Yarn, and lets you use different package manager versions across multiple projects without having to check-in the Yarn binary anymore.

Node.js >=16.10

Corepack is included by default with all Node.js installs, but is currently opt-in. To enable it, run the following command:

corepack enable

Node.js <16.10

Corepack isn't included with Node.js in versions before the 16.10; to address that, run:

npm i -g corepack

Updating the global Yarn version

Node.js ^16.17 or >=18.6

corepack prepare yarn@stable --activate

Node.js <16.17 or <18.6

Take a look at the latest Yarn release, note the version number, and run:

corepack prepare yarn@<version> --activate

Initializing your project

Just run the following command. It will generate some files inside your current directory; add them all to your next commit, and you'll be done!

yarn init -2

Note: By default, yarn init -2 will setup your project to be compatible with Zero-Installs, which requires checking-in your cache in your repository; check your .gitignore if you wish to disable this.

Note: In case you're migrating from Yarn 1.x and hit a blocker, you might want to take a look at our Migration Guide. It isn't always needed, but a fairly comprehensive resource of how to solve issues that may arise in the transition.

更新到最新版本

如果以后要将 Yarn 更新到最新版本,请运行:

yarn set version stable

Yarn will then configure your project to use the most recent stable binary. Don't forget to run a new install to update your artifacts before committing the results!

安装 master 分支的最新构建版本

From time to time even the most recent releases aren't enough, and you then will want to try out the very latest master branch to check if a bug has been fixed. This has become very simple! Just run the following command:

yarn set version from sources

同样的,还可以使用 --branch 参数来指定要安装特定的 PR:

yarn set version from sources --branch 1211