pnpmのメリット

ディスク容量の節約#

An illustration of the pnpm content-addressable store. 図には、node_modules を持つ2つのプロジェクトがあります。 The files in the node_modules directories are hard links to the same files in the content-addressable store.

npmを使用すると、ある依存関係を使用しているプロジェクトが100個ある場合、 ディスクにその依存関係のコピーが100個保存されます。 pnpmを使用すると、依存関係はコンテンツ探索可能なストアに格納され、

  1. 異なるバージョンのパッケージに依存している場合は、更新されたファイルのみがストアに追加されます。 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. すべてのファイルは、ディスク上の 1 つの場所に保存されます。 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.

インストール速度の向上#

pnpm performs installation in three stages:

  1. 依存関係の解決。必要な依存関係をすべて特定し、フェッチしてストアに格納します。
  2. ディレクトリ構造の計算。 The node_modules directory structure is calculated based on the dependencies.
  3. 依存関係をリンク。 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.

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.

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

フラットではない node_modules ディレクトリの作成#

npm や Yarn Classic を使用して依存パッケージをインストールする場合、すべてのパッケージはモジュールディレクトリの直下に格納されます。その結果、ソースコードは、プロジェクトへの依存関係として追加されていない依存パッケージにアクセスできてしまいます。

デフォルトでは、 pnpm はシンボリックリンクを使用して、プロジェクトの直接の依存関係のみをモジュールディレクトリの直下に追加します。

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

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. この設定によって pnpm は、npm や Yarn Classic と似た node_modules ディレクトリを作成するようになります。