pnpm 与 npm
npm 的扁平树
¥npm's flat tree
npm 从版本 3 开始维护 扁平化依赖树。这会减少磁盘空间的膨胀,并带来混乱的 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 的 修 剪命令 实现不允许你指定要修剪的包 - 它总是删除所有无关的和孤立的包。
¥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
.