Plug'n'Play vs nodeLinker
node_modules is a clever trick with a high price: thousands of small files copied per project, an install step that is mostly disk I/O, and a layout that lets any code require any hoisted package. Yarn Berry's Plug'n'Play (PnP) drops the folder entirely. Instead, Yarn writes one file, .pnp.cjs, that maps every package to its exact location (inside zip archives) and patches Node's resolver to use that map.
PnP in action
mkdir -p pnp && cd pnp
echo '{ "name": "pnp", "packageManager": "yarn@4.18.1" }' > package.json
touch yarn.lock
yarn add chalk@4.1.2 > /dev/null
ls -a
cat > ok.js <<'JS'
const chalk = require('chalk');
console.log('chalk resolved to', require.resolve('chalk'));
JS
node ok.js 2>&1 | grep -m1 Error
yarn node ok.js
node --require ./.pnp.cjs ok.js.
..
.pnp.cjs
.yarn
package.json
yarn.lock
Error: Cannot find module 'chalk'
chalk resolved to /home/learner/.yarn/berry/cache/chalk-npm-4.1.2-ba8b67ab80-10c0.zip/node_modules/chalk/source/index.js
chalk resolved to /home/learner/.yarn/berry/cache/chalk-npm-4.1.2-ba8b67ab80-10c0.zip/node_modules/chalk/source/index.js
- Plain
node ok.jsfails: there is nonode_modules, and Node's default resolver knows nothing else. yarn node(and everyyarn runscript) preloads.pnp.cjs, which teaches Node to resolvechalkto a path inside a zip file in the cache.node --require ./.pnp.cjsdoes the same by hand.- Nothing was unpacked. With the global cache (this machine's setting), the zip is shared by every project.
Strict by construction
PnP knows exactly which package declared which dependency, so an undeclared require is an error, not an accident waiting to happen:
mkdir -p pnp && cd pnp
echo '{ "name": "pnp", "packageManager": "yarn@4.18.1" }' > package.json
touch yarn.lock
yarn add chalk@4.1.2 > /dev/null
echo "require('supports-color');" > phantom.js
yarn node phantom.js 2>&1 | sed -n '5,9p'Error: Your application tried to access supports-color, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound.
Required package: supports-color
Required by: /home/learner/pnp/
supports-color is installed (chalk needs it), but your project never declared it. Under npm's hoisted node_modules the same line would quietly work, until chalk drops the dependency. The fix is yarn add supports-color.
Zero-installs: the cache in git
Turn off the global cache and Yarn keeps the zips inside the project, in .yarn/cache. Commit that folder (plus .pnp.cjs) and a fresh clone runs without any install step at all, which Yarn calls zero-installs:
mkdir -p pnp && cd pnp
echo '{ "name": "pnp", "packageManager": "yarn@4.18.1" }' > package.json
touch yarn.lock
yarn config set enableGlobalCache false
yarn add chalk@4.1.2 > /dev/null
ls .yarn/cache
cat .yarnrc.yml➤ YN0000: Successfully set enableGlobalCache to false
ansi-styles-npm-4.3.0-245c7d42c7-895a23929d.zip
chalk-npm-4.1.2-ba8b67ab80-4a3fef5cc3.zip
color-convert-npm-2.0.1-79730e935b-37e1150172.zip
color-name-npm-1.1.4-025792b0ea-a1a3f91415.zip
has-flag-npm-4.0.0-32af9f0536-2e789c61b7.zip
supports-color-npm-7.2.0-606bfcf7da-afb4c88521.zip
enableGlobalCache: false
Six small zips for chalk and its dependencies. The trade-off is repository size: every dependency update adds binary diffs to git history. Teams that use zero-installs like that CI and new clones need no registry access; teams that don't simply keep the global cache.
When a package needs real files: unplug
Some packages need to be real folders on disk: they ship native binaries, or they write next to their own files. Yarn detects most of them (via preferUnplugged or install scripts), and yarn unplug forces one:
mkdir -p pnp && cd pnp
echo '{ "name": "pnp", "packageManager": "yarn@4.18.1" }' > package.json
touch yarn.lock
yarn add chalk@4.1.2 > /dev/null
yarn unplug chalk > /dev/null
ls .yarn/unplugged
ls .yarn/unplugged/*/node_modules/chalk
grep -c unplugged .pnp.cjschalk-npm-4.1.2-ba8b67ab80
index.d.ts
license
package.json
readme.md
source
2
nodeLinker: opting out of PnP
Not every tool understands PnP. Bundlers do (esbuild, Webpack 5, Rollup with a plugin, Vite), TypeScript needs the editor SDKs (yarn dlx @yarnpkg/sdks vscode), and some older tools that read node_modules directly simply break. nodeLinker picks the install strategy per project:
nodeLinker |
Layout | Strictness |
|---|---|---|
pnp (default) |
.pnp.cjs + zip cache |
strict |
pnpm |
node_modules with symlinks into node_modules/.store, like pnpm |
strict-ish |
node-modules |
classic hoisted node_modules, like npm |
loose |
mkdir -p linkers && cd linkers
echo '{ "name": "linkers", "packageManager": "yarn@4.18.1" }' > package.json
touch yarn.lock
yarn config set nodeLinker node-modules > /dev/null
yarn add chalk@4.1.2 > /dev/null
echo "== node-modules"; ls node_modules; ls -a | grep -c pnp
echo "console.log(require('chalk').level !== undefined)" > ok.js && node ok.js
rm -rf node_modules
yarn config set nodeLinker pnpm > /dev/null
yarn install > /dev/null
echo "== pnpm"; ls -a node_modules
readlink node_modules/chalk== node-modules
ansi-styles
chalk
color-convert
color-name
has-flag
supports-color
0
true
== pnpm
.
..
.package-map.json
.store
chalk
.store/chalk-npm-4.1.2-ba8b67ab80/package
With node-modules, plain node works again and there's no .pnp.cjs; with pnpm, only chalk is visible at the top and it points into .store. Many teams adopt Yarn Berry with nodeLinker: node-modules first and switch to PnP once their tools cope.
Test yourself
Why does node ok.js fail in a PnP project while yarn node ok.js works?
- Yarn installs packages lazily
- There is no node_modules; yarn node preloads .pnp.cjs, which patches Node's resolver
- Node needs a flag for zip files
- PnP only works with ES modules
What does 'zero-installs' mean in Yarn Berry?
- Packages are never downloaded
- The zip cache and .pnp.cjs are committed, so a fresh clone works without running install
- Yarn installs nothing in CI
- Dependencies are bundled into the app
Which nodeLinker gives a classic, npm-like hoisted node_modules?
- pnp
- pnpm
- node-modules
- hoisted