モチベーション
Saving disk space
npmを使用すると、ある依存関係を使用しているプロジェクトが100個ある場合、 ディスクにその依存関係のコピーが100個保存されます。 pnpmを使用すると、依存関係はコンテンツ探索可能なストアに格納され、
- 異なるバージョンのパッケージに依存している場合は、更新されたファイルのみがストアに追加されます。 たとえば、100 個のファイルがある依存において、 新しいバージョンがそれらのファイルのうち 1 つだけに変更を加えた場合、
pnpm update
は存関係全体を複製するのではなく、ストアにその新しいファイルのみを加えます。 - すべてのファイルは、ディスク上の 1 つの場所に保存されます。 パッケージが インストールされると、そのパッケージのファイルは 1 か所からハードリンクされ、追加のディスク領域を消費しません。 これにより、同じバージョンの依存をプロジェクト間で共有できます。
これらの結果、 プロジェクトと依存関係の数に比例してディスク上の領域を節約し、インストールが非常に高速になります。
Boosting installation speed
pnpm perfoms installation in three stages:
- Dependency resolution. All required dependencies are identified and fetched to the store.
- Directory structure calculation. The
node_modules
directory structure is calculated based on the dependencies. - Linking dependencies. All remaining dependencies are fetched and hard linked from the store to
node_modules
.
This approach is significantly faster than the traditional three-stage installation process of resolving, fetching, and writing all dependencies 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 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.