Enabling Yarn Plug'n'Play (PnP) in a Node.js Project: A Step‑by‑Step Guide
Learn how to switch a Node.js project from the traditional node_modules layout to Yarn’s Plug'n'Play (PnP) system. The guide walks through prerequisites, config changes, installation, verification steps, and how to recover if a dependency fails.
10 Sept 2025, 02:32 UTC

Desired Outcome
Convert a classic node_modules layout to Yarn’s Plug’n’Play (PnP) system, eliminating the folder and speeding up install times while keeping dependency resolution deterministic.
Prerequisites
- Node.js 14+ (LTS is recommended).
- Yarn 2 or newer (Yarn 1 – “classic” – does not support PnP).
- Project already initialized with
yarn initor contains apackage.json. - Write permissions to the project root (no root or sudo required).
Procedure
- Verify Yarn version
Run
yarn --versionand ensure the output is >= 2.0.0.yarn --version # Expected: 2.x.x or newer - Create or update
.yarnrc.ymlPlace the following in the project root. If the file already exists, add or replace the
nodeLinkerkey.nodeLinker: pnp # Optional: enable a fallback to node_modules for problematic packages # pnpFallbackMode: node-modules - Install dependencies
Run
yarn install. Yarn will generate a.pnp.js(or.pnp.cjsin newer releases) instead of populatingnode_modules.yarn install - Verify PnP is active
- Check that
.pnp.jsexists in the project root. - Test a runtime import:
node -e "require('lodash')"should output nothing and exit with code 0. - Run
yarn why lodashand confirm the resolution path points to.pnp.jsrather thannode_modules.
# Verify file ls -l .pnp.js # Runtime test node -e "require('lodash')" # Yarn why yarn why lodash - Check that
- Handle native or __dirname‑dependent packages
Some packages that rely on
__dirnameor native bindings may fail. If you encounter such errors, consider adding a fallback:pnpFallbackMode: node-modulesThen re‑run
yarn install. - Test application and build scripts
Run your usual start or build commands (e.g.,
npm run startoryarn build) and watch for import errors. If everything compiles and starts, PnP is working.
Expected Checks
.pnp.jsfile present.- Runtime import test succeeds without errors.
- No
node_modulesfolder is created. - Yarn’s dependency graph shows PnP resolution (use
yarn why).
Recovery Options
- Revert to node_modules
Change
.yarnrc.ymlback tonodeLinker: node-modulesand runyarn installagain. This restores the classic layout.nodeLinker: node-modules - Isolate problematic packages
Use
pnpFallbackMode: node-modulesor add--ignore-enginesfor specific engines that conflict. - CI artifacts
Ensure your CI pipeline includes
.pnp.jsin the build artifacts; otherwise imports will fail. - Check peer dependencies
If PnP throws a missing peer dependency error, add the peer explicitly to
dependenciesordevDependencies.
Limitations & Notes
- Yarn PnP is not supported in Yarn 1 (“classic”). Verify you’re on Yarn 2+.
- Packages with native bindings may require re‑builds or patches to work under PnP.
- Some tooling (e.g., certain linters or test runners) may need configuration changes to recognize PnP.
- Always run a full test suite after enabling PnP to catch subtle runtime issues.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.