pnpm vs npm
npm 的扁平树
🌐 npm's flat tree
从版本 3 开始,npm 维护一个[扁平化依赖树]。这导致磁盘空间膨胀减少,副作用是产生一个凌乱的 node_modules 目录。
🌐 npm maintains a flattened dependency tree as of version 3. This leads to less
disk space bloat, with a messy node_modules directory as a side effect.
另一方面,pnpm 通过使用硬链接和符号链接来管理 node_modules,并将其存储在全局的磁盘内容可寻址存储中。这让你可以享受磁盘空间大大减少的好处,同时还能保持你的 node_modules 整洁。如果你想了解更多,可以查阅关于 [存储布局] 的文档。
🌐 On the other hand, pnpm manages node_modules by using hard linking and
symbolic linking to a global on-disk content-addressable store. This lets you get the benefits of far less disk space usage, while also keeping your
node_modules clean. There is documentation on the store layout if you wish
to learn more.
pnpm 的规范 node_modules 结构的好处在于它通过使无法使用未在项目 package.json 中指定的模块,来“帮助避免愚蠢的错误”。
🌐 The good thing about pnpm's proper node_modules structure is that it
"helps to avoid silly bugs" by making it impossible to use modules that are not
specified in the project's package.json.
安装
🌐 Installation
pnpm 不允许安装不保存到 package.json 的包。如果没有向 pnpm add 传递参数,包会作为常规依赖保存。与 npm 一样,--save-dev 和 --save-optional 可用于将包安装为开发或可选依赖。
🌐 pnpm does not allow installation of packages without saving them to
package.json. If no parameters are passed to pnpm add, packages are saved as
regular dependencies. Like with npm, --save-dev and --save-optional can be
used to install packages as dev or optional dependencies.
由于这一限制,项目在使用 pnpm 时不会有任何多余的包,除非它们移除了某个依赖并使其成为孤立依赖。这就是为什么 pnpm 的 [prune 命令] 实现不允许你指定要修剪的包——它总是会删除所有多余和孤立的包。
🌐 As a consequence of this limitation, projects won't have any extraneous packages when they use pnpm unless they remove a dependency and leave it orphaned. That's why pnpm's implementation of the prune command does not allow you to specify packages to prune - it ALWAYS removes all extraneous and orphaned packages.
目录依赖
🌐 Directory dependencies
目录依赖以 file: 前缀开头,并指向文件系统中的一个目录。像 npm 一样,pnpm 会为这些依赖创建符号链接。但与 npm 不同的是,pnpm 不会对文件依赖执行安装操作。
🌐 Directory dependencies start with the file: prefix and point to a directory in
the filesystem. Like npm, pnpm symlinks those dependencies. Unlike npm, pnpm
does not perform installation for the file dependencies.
这意味着,如果你有一个名为 foo(<root>/foo)的软件包,它依赖于 bar@file:../bar,当你在 foo 上运行 pnpm install 时,pnpm 不会为 <root>/bar 执行安装。
🌐 This means that if you have a package called foo (<root>/foo) that has
bar@file:../bar as a dependency, pnpm won't perform installation for
<root>/bar when you run pnpm install on foo.
如果你需要同时在多个包中运行安装,例如在单一代码库(monorepo)的情况下,你应该查看 pnpm -r 的文档。
🌐 If you need to run installations in several packages at the same time, for
instance in the case of a monorepo, you should look at the documentation for
pnpm -r.