Мотивация
Saving disk space#
При использовании npm, если у вас есть 100 проектов, использующих зависимость, у вас будет 100 копий этой зависимости, сохраненных на диске. С pnpm зависимость будет храниться в хранилище с адресацией по содержимому (CAS), поэтому:
- Если вы зависите от различных версий зависимости, в хранилище добавляются только файлы, которые отличаются друг от друга. For instance, if it has 100 files, and a new
version has a change in only one of those files,
pnpm updatewill only add 1 new file to the store, instead of cloning the entire dependency just for the singular change. - Все файлы сохраняются в одном месте на диске. When packages are
installed, their files are hard-linked or cloned (copy-on-write) from that single
place, consuming no additional disk space. By default, pnpm copies files only
when the filesystem supports neither. See
packageImportMethod. Это позволяет делиться зависимостями одной и той же версии между проектами.
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 performs installation in three stages:
- Dependency resolution. All required dependencies are identified and fetched to the store.
- Directory structure calculation. The
node_modulesdirectory structure is calculated based on the dependencies. - Linking dependencies. All remaining dependencies are fetched and hard linked from the store to
node_modules.
Each package moves to the next stage as soon as it is ready, without waiting for the other packages. This is faster than the traditional three-stage installation process, which resolves all dependencies, then fetches all of them, then writes all of them to node_modules.
Creating a non-flat node_modules directory#
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.
By default, pnpm uses symlinks to add only the direct dependencies of the project into the root of the modules directory.
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:
совет
If your tooling doesn't work well with symlinks, you may still use pnpm and set the nodeLinker setting to hoisted. This will instruct pnpm to create a node_modules directory that is similar to those created by npm and Yarn Classic.