Global Store and Hard Links

The Global Store and Hard Links

pnpm 69,400 's content-addressable store holds files, not packages, each saved once under a name derived from the SHA-512 hash of its contents, so two versions of a package share every unchanged file. Installing copies nothing: pnpm creates hard links, extra directory entries for the same data on disk (3).

One file in pnpm's store, hard-linked into two projects, reached through symlinks
One file in pnpm's store, hard-linked into two projects, reached through symlinks

The listing points two projects at a fresh store with --store-dir so the counts start from zero; normally you omit it and pnpm uses ~/.local/share/pnpm/store/v11 on Linux.

Two projects sharing one storeShell
mkdir -p ~/v5-ch1/store-demo/{a,b} && cd ~/v5-ch1/store-demo
cd a && echo '{"name":"a","private":true}' > package.json && pnpm add pg --store-dir ../store
cd ../b && echo '{"name":"b","private":true}' > package.json && pnpm add pg --store-dir ../store
cd .. && stat -c '%h links  %n' {a,b}/node_modules/.pnpm/pg@8.23.0/node_modules/pg/lib/client.js
du -sh store a b | expand -t 8
Output
...
Progress: resolved 14, reused 0, downloaded 14, added 14, done
...
Progress: resolved 14, reused 14, downloaded 0, added 14, done
...
3 links  a/node_modules/.pnpm/pg@8.23.0/node_modules/pg/lib/client.js
3 links  b/node_modules/.pnpm/pg@8.23.0/node_modules/pg/lib/client.js
1.9M    store
280K    a
280K    b

The second project reused all 14 packages (60 ms against 832 ms). client.js has three links, the store and both projects, and du, which counts each file once per run, charges 1.9 MB to the store and only 280 KB of directories and symlinks to each project. Hard links cannot cross file systems, so a project on another drive gets copies unless it has a store of its own.