Skip to main content

Corepack

通过阅读我们的 安装指南,你可能会注意到我们没有告诉你运行 npm install -g yarn 来安装 Yarn - 我们甚至不建议这样做。原因很简单:就像你的项目依赖必须锁定一样,包管理器本身也应该如此。

¥You may notice by reading our installation guide that we don't tell you to run npm install -g yarn to install Yarn - we even recommend against it. The reason is simple: just like your project dependencies must be locked, so should be the package manager itself.

将 Yarn 安装为全局二进制文件意味着你始终使用最新发布的版本。大多数时候它都运行良好,但偶尔会有一些内容会影响你的项目安装方式 - 无论是错误修复、新错误还是重大更改。

¥Installing Yarn as a global binary meant you always used whatever was the latest version published. Most of the time it worked fine, but every once in a while something was shipped that could impact the way your project was installed - be it a bugfix, new bug, or breaking change.

为了解决这个问题,Yarn 与 Node.js 项目联手开始开发 Corepack,这是一个官方的 Node.js 工具,可让你定义要在每个项目上使用哪个包管理器版本,就像你的锁文件对项目依赖所做的那样。

¥To counter that, Yarn joined forces with the Node.js project to start the development of Corepack, an official Node.js tool letting you define which package manager version you want to use on a per-project basis, just like your lockfile does for your project dependencies.

信息

Corepack 被标记为实验性的,因此我们可以更快地迭代其 CLI,但它已经是安装包管理器的首选方式 - 对于 Yarn 和 pnpm 都是如此。

¥Corepack is marked experimental so we can iterate on its CLI faster, but it's already the preferred way to install package managers - both for Yarn and for pnpm.

安装

¥Installation

从 Node.js 14.19 / 16.9 开始,所有官方 Node.js 版本都包含 Corepack。但是,在实验阶段期间它是可选的,因此你需要在它激活之前运行 corepack enable

¥Corepack is included with all official Node.js releases starting from Node.js 14.19 / 16.9. It's however opt-in for the duration of the experimental stage, so you'll need to run corepack enable before it's active.

你可以通过运行 yarn exec env 快速检查 Corepack 是否已启用:如果输出路径,则表示 Corepack 已正确安装。如果不是,则可能是某些东西干扰了垫片的安装方式。在这种情况下,请查看 故障排除 部分以获取建议。

¥You can quickly check whether Corepack is enabled by running yarn exec env: if you get a path as output, Corepack is properly installed. If not, something may be messing with how the shims are installed. In that case check the Troubleshooting section for advices.

危险

某些第三方分销商可能默认不包含 Corepack,特别是如果你从系统包管理器安装 Node.js。如果发生这种情况,在 corepack enable 之前运行 npm install -g corepack 应该可以解决问题。

¥Some third-party distributors may not include Corepack by default, in particular if you install Node.js from your system package manager. If that happens, running npm install -g corepack before corepack enable should do the trick.

故障排除

¥Troubleshooting

离线工作流程

¥Offline workflow

如果你使用隔离网络(公司 CI 中经常出现这种情况),你可能会遇到 Corepack 无法为活动项目下载 Yarn 二进制文件的问题。Corepack 文档包含一个 专用部分,解释了如何避免此问题。

¥If you use an airgapped network, as is often the case in company CIs, you may hit problems where Corepack isn't able to download the Yarn binary for the active project. The Corepack documentation includes a dedicated section explaining how to avoid this problem.

Volta

Volta 目前无法与 Corepack 集成,因为它会安装自己的垫片,从而阻止应用 Corepack 垫片。你需要运行以下两个命令来强制集成:

¥Volta doesn't currently integrate with Corepack, as it installs its own shims which prevent the Corepack ones from being applied. You will need to run the following two commands to force the integration:

npm install -g corepack

# Specifying an explicit install-directory makes corepack overwrite volta's yarn shims, which is what we want
corepack enable --install-directory ~/.volta/bin