Skip to main content
Version: 9.x

初衷

节省磁盘空间

¥Saving disk space

An illustration of the pnpm content-addressable store. On the illustration there are two projects with node_modules. The files in the node_modules directories are hard links to the same files in the content-addressable store.

使用 npm 时,如果你有 100 个使用依赖的项目,则磁盘上将保存该依赖的 100 个副本。使用 pnpm,依赖将存储在内容可寻址存储中,因此:

¥When using npm, if you have 100 projects using a dependency, you will have 100 copies of that dependency saved on disk. With pnpm, the dependency will be stored in a content-addressable store, so:

  1. 如果你依赖于不同版本的依赖,则仅将不同的文件添加到存储中。例如,如果它有 100 个文件,并且新版本仅对其中一个文件进行了更改,则 pnpm update 将仅向存储添加 1 个新文件,而不是仅针对单个更改克隆整个依赖。

    ¥If you depend on different versions of the dependency, only the files that differ are added to the store. For instance, if it has 100 files, and a new version has a change in only one of those files, pnpm update will only add 1 new file to the store, instead of cloning the entire dependency just for the singular change.

  2. 所有文件都保存在磁盘上的一个位置。安装软件包后,它们的文件从该位置进行硬链接,不消耗额外的磁盘空间。这允许你跨项目共享相同版本的依赖。

    ¥All the files are saved in a single place on the disk. When packages are installed, their files are hard-linked from that single place, consuming no additional disk space. This allows you to share dependencies of the same version across projects.

因此,你可以节省大量磁盘空间,与项目和依赖的数量成正比,并且安装速度更快!

¥As a result, you save a lot of space on your disk proportional to the number of projects and dependencies, and you have a lot faster installations!

提高安装速度

¥Boosting installation speed

pnpm 分三个阶段执行安装:

¥pnpm perfoms installation in three stages:

  1. 依赖解析。识别所有必需的依赖并将其提取到存储中。

    ¥Dependency resolution. All required dependencies are identified and fetched to the store.

  2. 目录结构计算。node_modules 目录结构是根据依赖计算出来的。

    ¥Directory structure calculation. The node_modules directory structure is calculated based on the dependencies.

  3. 链接依赖。所有剩余的依赖都会从存储中获取并硬链接到 node_modules

    ¥Linking dependencies. All remaining dependencies are fetched and hard linked from the store to node_modules.

An illustration of the pnpm install process. Packages are resolved, fetched, and hard linked as soon as possible.

这种方法比解析、获取所有依赖并将所有依赖写入 node_modules 的传统三阶段安装过程要快得多。

¥This approach is significantly faster than the traditional three-stage installation process of resolving, fetching, and writing all dependencies to node_modules.

An illustration of how package managers like Yarn Classic or npm install dependencies.

创建非扁平 node_modules 目录

¥Creating a non-flat node_modules directory

使用 npm 或 Yarn Classic 安装依赖时,所有包都会提升到模块目录的根目录。因此,源代码可以访问未作为依赖添加到项目中的依赖。

¥When installing dependencies with npm or Yarn Classic, all packages are hoisted to the root of the modules directory. As a result, source code has access to dependencies that are not added as dependencies to the project.

默认情况下,pnpm 使用符号链接仅将项目的直接依赖添加到模块目录的根目录中。

¥By default, pnpm uses symlinks to add only the direct dependencies of the project into the root of the modules directory.

An illustration of a node_modules directory created by pnpm. Packages in the root node_modules are symlinks to directories inside the node_modules/.pnpm directory

如果你想了解有关 pnpm 创建的独特 node_modules 结构以及它为何能够与 Node.js 生态系统良好配合的更多详细信息,请阅读:

¥If you'd like more details about the unique node_modules structure that pnpm creates and why it works fine with the Node.js ecosystem, read:

提示

如果你的工具不能很好地处理符号链接,你仍然可以使用 pnpm 并将 node-linker 设置设置为 hoisted。这将指示 pnpm 创建一个与 npm 和 Yarn Classic 创建的目录类似的 node_modules 目录。

¥If your tooling doesn't work well with symlinks, you may still use pnpm and set the node-linker setting to hoisted. This will instruct pnpm to create a node_modules directory that is similar to those created by npm and Yarn Classic.