Yarn 对你的代码来说是一个软件包管理器, 你可以通过它使用全世界开发者的代码,或者分享自己的代码给全世界的开发者。Yarn 做这些事情快捷、安全、可靠,所以你不用担心什么。
通过 Yarn 你可以使用其他开发者针对不同问题的解决方案,简化你开发软件的过程。 如果使用过程中遇到问题,你可以将其上报或者贡献解决方案。一旦问题被修复, 你可以使用 Yarn 更新。
代码通过 软件包(package) 的方式被共享。一个软件包里包含了所有需要共享的代码,以及一个描述软件包信息的文件 package.json
(叫做 清单)。
2 - 安装
从 Yarn 1 迁移
我们一直在编纂 迁移指南 为从 Yarn 1 迁移到 Yarn 2 提供有用的建议。请查看此指南,如果发现尚未涵盖的内容请反馈!如果遇到问题,请查阅 PnP 兼容列表 和 开启 node-modules 插件!
全局安装
Using a single package manager across your system has always been a problem. To be stable, installs need to be run with the same package manager version across environments, otherwise there's a risk we introduce accidental breaking changes between versions - after all, that's why the concept of lockfile was introduced in the first place! And with Yarn being in a sense your very first project dependency, it should make sense to "lock it" as well. 在整个系统中使用单个包管理器一直是个问题。为了稳定,安装需要跨环境使用相同的包管理器版本运行,否则我们有可能在版本之间引入意外中断更改 - 毕竟,这就是为什么首先引入了锁文件的概念!在某种意义上,Yarn 是您的第一个项目依赖项,因此"锁定它"也是有道理的。
For this reason, Yarn 2 and later are meant to be managed on a by-project basis. Don't worry, little will change! Just make sure to first install the global Yarn binary that we will use to spawn the local ones: 因此,Yarn 2 及更高版本应按项目管理。别担心,改变很小!只需确保首先安装全局 Yarn 二进制文件,我们将用于生成本地二进制:
npm install -g yarn
Once you've followed the instructions (running yarn --version
from your home directory should yield something like 1.22.0
), go to the next section to see how to actually enable Yarn 2 on your project. 当你你遵循上述说明(从主目录运行应该产生类似),请转到下一节,了解如何在项目中实际启用 Yarn 2。yarn --version1.22.0
You've probably remarked the global Yarn is from the "Classic" line (1.x). This is expected! One extra perk of this system is that projects configured for Yarn 1 will keep using it instead of suddenly having to migrate to the 2.x configuration format. We wouldn't have had to do this if Yarn had been "project locked" from the beginning, but hindsight is 20/20 😉
按项目安装
-
遵循全局安装的说明
-
进入你的项目目录:
cd ~/path/to/project
- 运行以下命令:
yarn policies set-version berry # 针对小于 v1.22 版本
yarn set version berry # 针对 v1.22+ 版本
"Berry" 是 Yarn 2 发布线的代号,同时也是我们的 代码仓库 的名称!
- 提交更改后的
.yarn
和.yarnrc.yml
文件
更新到最新版本
如果以后要将 Yarn 更新到最新版本,请运行:
yarn set version latest
Yarn 将从我们的网站下载最新的二进制文件,并将其安装在您的项目中。然后,不要忘记更新依赖项并提交更新结果!
安装 master 分支的最新构建版本
随着时间的推移,即使最新版本也不够新了,那么你可能希望尝试最新的 master 代码分支,以检查 Bug 是否已修复。这在 Yarn 2 中已经变得非常简单了!只需运行以下命令即可:
yarn set version from sources
同样的,还可以使用 --branch
参数来指定要安装特定的 PR:
yarn set version from sources --branch 1211
显示命令列表
yarn help
初始化一个新项目
yarn init
安装所有依赖项
yarn
yarn install
添加依赖项
yarn add [package]
yarn add [package]@[version]
yarn add [package]@[tag]
将依赖项添加到不同的依赖类别中
yarn add [package] --dev # dev dependencies
yarn add [package] --peer # peer dependencies
更新依赖项
yarn up [package]
yarn up [package]@[version]
yarn up [package]@[tag]
删除依赖项
yarn remove [package]
更新 Yarn 本体
yarn set version latest
yarn set version from sources