Learning navigation

Code with Animation

Yarn & Plug'n'Play (PnP / Zero Installs)

Discover how Yarn PnP uses a generated dependency loader instead of the usual node_modules lookup, and how a configured project cache supports zero-installs workflows.

Package Managers4 min readConcept 13 of 24

What is Yarn Plug'n'Play (PnP)?

Yarn Plug'n'Play (PnP) is an alternative module resolution strategy introduced in Yarn Berry (v2+) that completely eliminates the node_modules folder from Node.js projects.

PnP generates a loader named .pnp.cjs with dependency locations and relationships. Package archives can be reused from Yarn's cache; a project-local .yarn/cache is a configuration choice rather than the universal default.

Why eliminate node_modules and move to Zero Installs?

Traditional node_modules folders create massive overhead: disk operations (thousands of fs.stat calls) slow down application startup, file copying during installs takes minutes, and flat hoisting introduces dangerous phantom dependencies.

A zero-installs workflow combines PnP with a committed project-local cache and loader data. This can avoid repeated fetching or linking after checkout, but does not eliminate compilation, native-package setup or every environment-specific step.

How Yarn PnP Resolves Packages at Runtime

Yarn configures its generated PnP loader so Node and supported tools can resolve declared packages using the dependency map and cache locations.

When code loads a package, PnP resolves it using the importing package's dependency relationships. It can report undeclared dependencies instead of relying on an incidental hoisted node_modules layout.

Inspect Yarn PnP and Test In-Memory Resolution

Experience how Yarn PnP resolves dependencies directly from zip archives without a node_modules folder.

// Traditional node_modules Resolution (Legacy)
$ node server.js
// Recursive Directory Traversal on Disk
require("lodash");
// System Calls (O(N) Disk Operations)
1. fs.stat("./node_modules/lodash") -> ENOENT
2. fs.stat("../node_modules/lodash") -> ENOENT
3. fs.stat("/app/node_modules/lodash") -> FOUND!
// Result: 100,000+ unzipped files on disk
// Phantom import vulnerabilities allowed
Legacy node_modules Disk Hierarchy
node_modules/ (140 MB, 120,000 files)
express/
node_modules/
accepts/
lodash/ hoisted
Disk Bottleneck: Scanning folders on disk takes thousands of I/O operations. Code can access hoisted sub-dependencies directly, creating dangerous phantom dependencies.

Step through Yarn PnP architecture to compare node_modules vs .pnp.cjs resolution

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1What is the role of .pnp.cjs in a Yarn PnP project?
  2. 2PnP reports that your package tried to import an undeclared dependency. What is the normal fix?
  3. 3Which statement about Yarn zero-installs is accurate?

Remember this

  • PnP uses a generated .pnp.cjs dependency loader instead of conventional node_modules traversal.
  • Zero-installs requires a deliberate committed-cache workflow; native setup or builds may still be needed.
  • PnP prevents phantom dependencies by strictly enforcing declared dependencies in package.json at resolution time.
  • Use supported editor integration and yarn node to run Node with the project's PnP hook.

Done with this concept?

Mark it complete to track your progress. No login needed.