pnpm対Yarn:CIにはどちらのパッケージマネージャーか?
どちらもnpmの現代的な代替ですが、pnpmとYarnはディスク、厳格さ、workspaceで異なるトレードオフを行います。
pnpmはコンテンツアドレス方式のストアとシンボリックリンクされたnode_modulesを使います。Yarn(Classicまたは新しいBerry)は高速なインストール、workspace、そしてオプションのPlug'n'Playを提供します。CIではその差がディスク使用量とcacheの挙動に現れます。
pnpm vs Yarn at a glance
| pnpm | Yarn | |
|---|---|---|
| node_modulesのレイアウト | シンボリックリンクされたコンテンツアドレスストア | フラット(Classic)またはPnP(Berry) |
| ディスク使用量 | 低い(共有ストア) | 高め(Classic)、低い(PnP) |
| 厳格さ | 厳格(ファントム依存なし) | 緩い(Classic)、厳格(PnP) |
| CIインストール | pnpm i --frozen-lockfile | yarn install --immutable |
| monorepo workspace | 強力 | 強力 |
| ツールとの互換性 | 高い | 高い (classic)、低い (PnP) |
| CIでのlockfileの挙動 | CIでは自動的にfrozen | Berryでは --immutable |
| Corepack | 対応、Node 26で標準 | 対応、Node 26で標準 |
CIにおいて
pnpmはディスクとウォームストアのインストール速度で勝る傾向があり、その厳格な解決はファントム依存のバグが本番に到達する前に捕捉します。Yarn BerryのPnPも非常に高速でスリムになり得ますが、PnPは一部のパッケージが想定していない形でモジュール解決を変えるため、依存ツリーをテストしましょう。Yarn Classicは、すでに使っているなら最も摩擦の少ないドロップインです。
- A package that imports a transitive dependency it never declared works under Yarn classic and fails under pnpm.
- That failure is correct: the import was always a latent bug and Yarn hoisting was hiding it.
- It is also the single most common reason a Yarn-to-pnpm migration is more work than expected, so budget for it rather than being surprised.
- Yarn PnP is strict in the same way, so migrating Yarn classic to Yarn PnP surfaces exactly the same class of breakage.
storeをcacheする
pnpmではstoreディレクトリ(pnpm store path)をpnpm-lock.yamlをキーにcacheします。YarnではYarnのcacheフォルダをyarn.lockをキーにcacheします。storeをcacheすることは、ツールの選択そのものよりもwall-clock時間にとって重要です。
- pnpm switches to frozen-lockfile mode automatically when it detects CI, so you do not need to pass the flag.
- Since pnpm 11 an install fails outright if the lockfile was written by a newer pnpm major version, rather than silently rewriting it. That is a correctness improvement and also a new way for CI to go red after a colleague upgrades locally.
- pnpm documents that caching the store makes installation faster in most scenarios but is not required and not guaranteed to help, so measure rather than assume.
- name: Install pnpm and Node.js
uses: pnpm/setup@v2.0.0
with:
runtime: node@${{ matrix.node-version }}
cache: true # caches the pnpm store between runs
- run: pnpm install # frozen-lockfile is automatic in CIYarn PnP: 得るものと手放すもの
Plug and Playはこの比較で最も興味深い発想であり、トレードオフが最も鋭いものでもあります。node_modules をなくすことは、インストールで最も遅い部分、つまり何万もの小さなファイルを書き出す処理をなくすことです。
- 得るもの: より速いインストール、はるかに少ないディスク使用量、そしてphantom dependencyを遮断する厳密な解決。
- 代償: ディスク上の
node_modulesを走査するツールはPnPへの対応が必要です。これはいまだにバンドラ、エディタ、デバッグ用ツールのロングテールで問題になります。 - 実務上の帰結として、PnPはツールチェーンを完全に掌握しているコードベースには強く適合し、掌握していない広いplugin面を持つコードベースには適しません。
Monorepo
どちらも成熟したworkspace実装を持ち、実際の意思決定の大半はここで起きます。
- pnpmの
--filterは、名前、パス、あるいはgit refに対する変更でパッケージを選べます。これは「このPRが触れたものだけをビルドする」に直接対応します。 - pnpmのcatalogを使うと依存バージョンを一度宣言してすべてのworkspaceパッケージから参照でき、バージョンのずれを直すPRという一群を丸ごとなくせます。
- pnpmの厳密さはmonorepoでこそ最も価値があります。パッケージ間の意図しないimportは、まさに防ごうとしている結合そのものだからです。
- Yarnのworkspaceは長く確立され、よく理解されており、既存のYarn monorepoにとって今も十分に良い選択です。
既存のYarnリポジトリを移行すべきか?
- Yarn classicで問題なく動いているなら、正直な答えはたいてい「いいえ」です。移行コストは実在し、見返りの大半はCIのminuteとディスクです。
- monorepo全体でphantom dependencyのバグやバージョンのずれに悩まされているなら、pnpmの厳密さとcatalogが原因そのものに対処するので、移行する価値があります。
- 新しいmonorepoを始めるなら、pnpmのworkspaceとcatalogが今日いちばん摩擦の少ない道です。
- Yarn BerryのPnPで動いているなら、そのままにしてください。すでに厳密な解決とディスク効率を得ており、pnpmへの移行は横滑りになります。
- 何を選ぶにせよ、Corepackで固定し、runnerとすべての開発マシンが一致するようにしてください。
Decide with your own numbers, not a feature table
Feature comparisons age badly and rarely decide anything, because both tools in a mature category can do the job. What differs is how each behaves on your repository, and that takes one afternoon to measure.
# time a cold install with each candidate, cache cleared
hyperfine --prepare "rm -rf node_modules" --warmup 1 \
"<tool-a> install" "<tool-b> install"
# and the thing CI actually pays for: a cold run with no local cache
docker run --rm -v "$(pwd):/w" -w /w node:22 sh -c "<tool> install"What actually changes when you switch
- Lockfile format. A switch is a one-way door for anyone still on the old tool until everyone migrates, so plan it as a single coordinated change.
- Resolution strictness. Tools differ on whether an undeclared transitive import works, and the stricter one will surface latent bugs as new failures.
- CI cache configuration. The cache path and key differ per tool; carrying over the old ones silently disables caching.
- Everyone on the team and every runner must move together. Pin the version so they cannot drift.
The verdict
低ディスクと厳格な解決を望むなら:pnpm。高速なドロップイン、あるいはPnPのzero-installモデルを望み、すでにYarnを使っているなら:Yarn。どちらでもlockfileをコミットし、storeをcacheしましょう。
よくある質問
pnpmはYarnより速いですか?
なぜpnpmはディスクを節約するのですか?
Yarnからpnpmに切り替えるとなぜコードが壊れるのですか?
node_modules を平坦化するため、宣言していないものをパッケージがimportできてしまいます。pnpmは平坦でない厳密な構成を作るので、そのimportは失敗します。この失敗は正しく、直し方は解決を緩めることではなく、欠けている依存を宣言することです。CIでpnpmに --frozen-lockfile は必要ですか?
GitHub Actionsでpnpmのストアをcacheすべきですか?
pnpm/setup actionは cache: true でそれを行います。pnpmはこれが多くの場面でインストールを速くすると文書化していますが、必須ではなく効果が保証されるわけでもないので、自分のインストール時間を両方の設定で測ってください。Yarn PnPは使う価値がありますか?
node_modules をなくすことで、より速いインストール、はるかに少ないディスク、厳密な解決が得られます。代償は、ディスク上の node_modules を前提とするものはPnP対応が必要で、これがいまだにロングテールの一部でつまずくことです。Yarnの nodeLinker: node-modules が中間の道です。monorepoにはpnpmとYarnのどちらですか?
--filter がgit refに対する変更でパッケージを選べ、catalogがパッケージ横断で依存バージョンを集約し、厳密な解決がパッケージ間の意図しないimportを防ぎます。問題なく動いている既存のYarn workspaceは、解決すべき問題ではありません。CIが自分のマシンと同じパッケージマネージャのバージョンを使うようにするには?
package.json の packageManager フィールドにバージョンを宣言してください。開発マシンとrunnerのバージョンのずれは、lockfile関連のCI失敗のほとんどの原因であり、固定することでその分類ごとなくなります。