Skip to main content

安装模式

Yarn 支持三种不同的方式在磁盘上安装项目。本文档简要概述了它们,以及每种解决方案的优缺点。

¥Yarn supports three different ways to install your projects on disk. This document gives a quick overview of them all, along with the pros and cons of each.

信息

所有安装模式都完全稳定。Yarn 默认使用 PnP 安装,但 pnpm 和 node-modules 链接器也是一等公民,用于生产设置。

¥All install modes are fully stable. Yarn uses PnP installs by default, but the pnpm and node-modules linkers are first-class citizens as well, used in production settings.

nodeLinker: pnp

Plug'n'Play 安装在其 专用文档 中有更详细的描述,但现在你可以将它们视为为每个项目生成一个 Node.js 加载器文件,这使你的工具可以直接从磁盘上的全局存储中访问包。不需要文件副本,甚至不需要符号链接/硬链接。

¥Plug'n'Play installs are described in more details in their dedicated documentation, but for now you can think of them as generating a single Node.js loader file for each of your projects, which makes your tools directly access packages from a global store on your disk. No need for file copies, or even symlinks / hardlinks.

优点缺点
非常快不太符合惯用语
内容可寻址存储IDE 集成通常需要 SDKs
防止幽灵依赖有时需要 packageExtensions
语义依赖错误
完美提升优化
提供 依赖树 API
可以升级到 zero-installs
信息

自 2019 年以来,Yarn Plug'n'Play 一直是 Yarn 中的默认安装策略,随着我们与工具作者合作以消除边缘,兼容性故事多年来显着改善。

¥Yarn Plug'n'Play has been the default installation strategy in Yarn since 2019, and the compatibility story significantly improved along the years as we worked with tooling authors to smoothen the edges.

nodeLinker: pnpm

在此模式下,在 node_modules/.store 中生成一个扁平文件夹,其中包含项目中的每个依赖的一个文件夹。每个依赖文件夹都填充了从系统上所有项目共用的中央存储(默认为 $HOME/.yarn/berry/index)获得的硬链接。最后,从扁平存储到相关文件夹的符号链接被放入 node_modules 文件夹中。

¥Under this mode, a flat folder is generated in node_modules/.store containing one folder for each dependency in the project. Each dependency folder is populated with hardlinks obtained from a central store common to all projects on the system (by default $HOME/.yarn/berry/index). Finally, symlinks to the relevant folders from the flat store are placed into the node_modules folders.

优点缺点
比 PnP 慢,但仍然非常快工具并不总是支持符号链接
内容可寻址存储硬链接可能导致奇怪的行为
防止某些幽灵依赖通用依赖错误
不需要 IDE SDK有时需要 packageExtensions
信息

pnpm 模式是传统 node_modules 安装和更现代的 Yarn PnP 安装之间的一个有趣的中间地带;它不会降低性能太多,并提供稍微好一点的兼容性,但代价是失去了一些有趣的功能。

¥The pnpm mode is an interesting middle ground between traditional node_modules installs and the more modern Yarn PnP installs; it doesn't decrease the performances much and provides a slightly better compatibility story, at the cost of losing a couple of interesting features.

nodeLinker: node-modules

此模式是安装 Node.js 项目的旧方法,由 Node.js 和几乎整个 JavaScript 生态系统原生支持。

¥This mode is the old tried and true way to install Node.js projects, supported natively by Node.js and virtually the entirety of the JavaScript ecosystem.

虽然我们倾向于建议首先尝试其他两种模式中的一种,但如果你遇到依赖问题而现在没有时间解决,它仍然是一个可靠的选择。当然,你的项目可能会稍微不稳定一些,因为你不会注意到幽灵依赖是否悄悄出现,但根据情况,这可能是一个合理的权衡。

¥While we tend to recommend trying one of the two other modes first, it remains a solid option in case you face problems with your dependencies that you don't have the time to address right now. Sure, your project may be a little more unstable as you won't notice if ghost dependencies creep in, but it may be a reasonable trade-off depending on the circumstances.

优点缺点
与整个生态系统完美兼容平均速度
对硬链接 (nmMode) 的可选支持没有针对幽灵依赖的保护
不需要 IDE SDK由于文件系统依赖导致提升不完美