TExcelTAD

The Content-Addressable Store

npm and Yarn classic copy every package into every project. Ten projects using lodash means ten copies of its 600 files. pnpm keeps one copy of each file on the machine, in a content-addressable store, and builds each project's node_modules from hard links and symlinks pointing into it. Installs get faster, disks stay smaller, and the layout turns out to be stricter too.

Where the store lives

bash · try it yourself
pnpm store path
mkdir -p a && cd a
echo '{ "name": "a", "version": "1.0.0" }' > package.json
pnpm add lodash@4.17.21
ls "$(pnpm store path)"
ls "$(pnpm store path)/files" | head -3
ls "$(pnpm store path)/files/00" | head -2
/home/learner/.local/share/pnpm/store/v10
Progress: resolved 1, reused 0, downloaded 0, added 0
Packages: +1
+
Progress: resolved 1, reused 0, downloaded 1, added 1, done

dependencies:
+ lodash 4.17.21 (4.18.1 is available)

Done in 832ms using pnpm v10.34.7
files
index
projects
00
01
02
0af4fc03d2ecf7155b911e07941ea071519cc573fc911548cdad0701587a3617d6dc9954610260c3d1c421adc4799ccec5835ec14b994d5fc5baa8a6f83e0a
1964986d531b57f13008a726001a1e7999d284cf8d03725c57160bc19406a20f8c508b10f838f325f2584daafdbc996c2dc6b0a82a0a8d67ec53898625d83c

"Content-addressable" means a file's location is derived from its content: each file is stored under the SHA-512 hash of its bytes (the first two hex digits are the folder name). Two packages that contain an identical file share one entry, and a corrupted file is detectable because its hash no longer matches its name. index/ maps each package@version to its list of file hashes.

Hard links: one file, many names

A hard link is a second directory entry for the same file on disk (the same inode). It takes no extra space; ls -l and stat show the link count. Install the same lodash into a second project and watch the count go up:

bash · try it yourself
mkdir -p a b
cd a && echo '{ "name": "a", "version": "1.0.0" }' > package.json && pnpm add lodash@4.17.21 > /dev/null && cd ..
F=node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/chunk.js
ls -l a/$F
stat -c '%h links, %s bytes' a/$F
cd b && echo '{ "name": "b", "version": "1.0.0" }' > package.json
pnpm add lodash@4.17.21 2>&1 | grep -E 'reused|^\+'
cd ..
stat -c '%h links, %s bytes' a/$F
[ a/$F -ef b/$F ] && echo "a and b point at the same inode"
-rw-r--r-- 2 learner learner 1411 Oct 23 17:47 a/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/chunk.js
2 links, 1411 bytes
Progress: resolved 1, reused 0, downloaded 0, added 0
+
Progress: resolved 1, reused 1, downloaded 0, added 1, done
+ lodash 4.17.21 (4.18.1 is available)
3 links, 1411 bytes
a and b point at the same inode

After project b's install the file has three names: the store's, a's and b's. [ x -ef y ] is the shell's "same file" test. pnpm reported reused 1, downloaded 0: nothing was fetched or copied. Editing a file inside node_modules would edit it for every project at once, which is one more reason never to patch node_modules by hand (use pnpm patch instead).

Note: Hard links only work within one file system. If the store and the project are on different drives, pnpm falls back to copying (or reflinks/clones on file systems that support them, such as APFS and Btrfs); set package-import-method to control it.

Files are shared across versions too

Deduplication is per file, not per package. lodash 4.18.1 has hundreds of files byte-for-byte identical to 4.17.21:

bash · try it yourself
STORE=$(pnpm store path)
mkdir -p a c
cd a && echo '{ "name": "a", "version": "1.0.0" }' > package.json && pnpm add lodash@4.17.21 > /dev/null && cd ..
echo "store files after lodash 4.17.21: $(find $STORE/files -type f | wc -l)"
cd c && echo '{ "name": "c", "version": "1.0.0" }' > package.json && pnpm add lodash@4.18.1 > /dev/null && cd ..
echo "store files after lodash 4.18.1:  $(find $STORE/files -type f | wc -l)"
echo "files in lodash 4.18.1:           $(find c/node_modules/.pnpm/lodash@4.18.1 -type f | wc -l)"
[ a/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/chunk.js -ef c/node_modules/.pnpm/lodash@4.18.1/node_modules/lodash/chunk.js ] && echo "chunk.js is shared between the two versions"
store files after lodash 4.17.21: 1036
store files after lodash 4.18.1:  1050
files in lodash 4.18.1:           1051
chunk.js is shared between the two versions

A new version cost only the files that actually changed.

Symlinks: the node_modules layout

Inside a project, pnpm builds a two-level layout:

bash · try it yourself
mkdir -p p && cd p
echo '{ "name": "p", "version": "1.0.0" }' > package.json
pnpm add chalk@4.1.2 > /dev/null
ls -l node_modules | grep -- '->' | sed 's/.*<MTIME> //; s/.*[0-9][0-9]:[0-9][0-9] //'
ls node_modules/.pnpm
ls -l node_modules/.pnpm/chalk@4.1.2/node_modules | grep -- '->' | sed 's/.*[0-9][0-9]:[0-9][0-9] //'
chalk -> .pnpm/chalk@4.1.2/node_modules/chalk
ansi-styles@4.3.0
chalk@4.1.2
color-convert@2.0.1
color-name@1.1.4
has-flag@4.0.0
lock.yaml
node_modules
supports-color@7.2.0
ansi-styles -> ../../ansi-styles@4.3.0/node_modules/ansi-styles
supports-color -> ../../supports-color@7.2.0/node_modules/supports-color
  1. node_modules/chalk is a symlink to .pnpm/chalk@4.1.2/node_modules/chalk. Only your direct dependencies appear at the top.
  2. node_modules/.pnpm/<name>@<version>/node_modules/ is a virtual store folder per package. It holds the package itself (hard-linked files) and symlinks to that package's own dependencies (ansi-styles, supports-color) as siblings.

When chalk's code calls require('supports-color'), Node resolves the symlink to its real path, .pnpm/chalk@4.1.2/node_modules/chalk/, walks up one level to .pnpm/chalk@4.1.2/node_modules/, and finds supports-color there. It's the ordinary resolution walk, with no runtime patch like Yarn's PnP. Every package sees exactly its declared dependencies, and two versions of one package never fight over a top-level slot.

A store model

js · try it yourself
// A tiny content-addressable store: files are stored by the hash of their content.
const hash = (s) => [...s].reduce((h, c) => (h * 31 + c.charCodeAt(0)) >>> 0, 7).toString(16).padStart(8, '0');

const store = new Map(); // hash -> content
const index = {};        // "pkg@version" -> { path: hash }

function addPackage(id, files) {
  let added = 0;
  index[id] = {};
  for (const [path, content] of Object.entries(files)) {
    const h = hash(content);
    if (!store.has(h)) { store.set(h, content); added++; }
    index[id][path] = h;
  }
  console.log(`${id.padEnd(16)} ${Object.keys(files).length} files, ${added} new in store, store size ${store.size}`);
}

addPackage('lodash@4.17.21', { 'chunk.js': 'chunk v1', 'map.js': 'map v1', 'package.json': '{"version":"4.17.21"}' });
addPackage('lodash@4.17.21', { 'chunk.js': 'chunk v1', 'map.js': 'map v1', 'package.json': '{"version":"4.17.21"}' });
addPackage('lodash@4.18.1', { 'chunk.js': 'chunk v1', 'map.js': 'map v2', 'package.json': '{"version":"4.18.1"}' });
console.log(index['lodash@4.18.1']['chunk.js'] === index['lodash@4.17.21']['chunk.js'] ? 'chunk.js shared' : 'chunk.js copied');
lodash@4.17.21   3 files, 3 new in store, store size 3
lodash@4.17.21   3 files, 0 new in store, store size 3
lodash@4.18.1    3 files, 2 new in store, store size 5
chunk.js shared

Maintaining the store

Command Does
pnpm store path print the location (per drive; store-dir changes it)
pnpm store status check that no file in the store was modified
pnpm store prune delete packages no project references any more
pnpm install --offline / --prefer-offline use only / prefer the store, skipping the network

In CI, cache the store directory (keyed on pnpm-lock.yaml) rather than node_modules, for the same reason as ~/.npm with npm ci.

Test yourself

What determines where a file is stored in pnpm's content-addressable store?

  1. The package name
  2. The hash of the file's contents
  3. The install date
  4. The project that installed it

Why does installing the same package into a second project take almost no extra disk space with pnpm?

  1. pnpm compresses node_modules
  2. The files are hard links to the same inodes in the store
  3. pnpm installs into a global folder only
  4. It downloads a smaller tarball

In a pnpm project, where does chalk find its dependency supports-color?

  1. In the top-level node_modules
  2. As a sibling symlink in node_modules/.pnpm/chalk@4.1.2/node_modules/
  3. Through a .pnp.cjs map
  4. In the global store directly