# 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.

Subject: Package Managers | Lesson 13 of 24 | 4 min read
Interactive lesson: https://codewithanimation.in/package-managers/yarn-and-plug-n-play
Previous: [npm Scripts & Lifecycle Hooks](https://codewithanimation.in/package-managers/npm-scripts-and-lifecycle.md) | Next: [pnpm & The Content-Addressable Store](https://codewithanimation.in/package-managers/pnpm-content-addressable-store.md)

## 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.

## Common mistake: Undeclared transitive imports and unconfigured developer tooling

Because Yarn PnP strictly enforces declared dependencies in `package.json`, calling `require()` on a package that exists in your dependency tree but is not listed in your manifest will throw a strict resolution error. Additionally, legacy IDE extensions or build tools that manually scan for physical `node_modules` directories will fail unless updated with Yarn Editor SDKs (`yarn dlx @yarnpkg/sdks vscode`).

## Tip: Use Editor SDKs for IDE support and .pnp.loader.mjs for ESM modules

Use Yarn's documented editor SDK setup for compatible IDE integration. For a Node script, yarn node ./my-script.js runs Node with the PnP hook configured; avoid copying conflicting manual loader flags between Node/Yarn versions.

## Try it: Inspect Yarn PnP and Test In-Memory Resolution

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

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/package-managers/yarn-and-plug-n-play

## Check yourself

### Question 1

What is the role of .pnp.cjs in a Yarn PnP project?

- A. A loader describing dependency locations and resolution
- B. It is the application's compiled CSS
- C. It is a required public registry password
- D. It forces all packages into a global node_modules

Correct answer: A

PnP uses a generated loader instead of relying on the usual node_modules traversal. Yarn can also be configured to use other install strategies.

- Why not B: The PnP loader handles package resolution.
- Why not C: The loader is not an authentication secret.
- Why not D: PnP replaces that conventional lookup layout.

### Question 2

PnP reports that your package tried to import an undeclared dependency. What is the normal fix?

- A. Disable every package integrity check
- B. Declare the dependency in the package that actually uses it
- C. Assume the dependency is usable because another workspace declares it
- D. Rename .pnp.cjs to package.json

Correct answer: B

PnP can identify ghost dependencies instead of accepting incidental hoisting. Fix the responsible package declaration or an appropriate package extension for faulty third-party metadata.

- Why not A: Integrity checking is a different concern.
- Why not C: Declarations belong to the package that uses the dependency.
- Why not D: The loader and manifest serve different roles.

### Question 3

Which statement about Yarn zero-installs is accurate?

- A. Every Yarn project automatically commits its cache
- B. It guarantees every application compiles in zero seconds
- C. A committed project cache and PnP data can avoid many installs, but native builds may still need work
- D. Native packages never need machine-specific build steps

Correct answer: C

Zero-installs requires deliberate cache/configuration choices. PnP alone does not promise zero-second builds or eliminate native setup on every machine.

- Why not A: A project-local committed cache is a configuration/workflow choice.
- Why not B: Dependency availability is different from compilation time.
- Why not D: Native dependencies can still require installation/build work.

## 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.
