Yarn PnP nodeLinker configuration for native binary compatibility
0 reputation · 27 Aug 2020, 13:29 UTC
0 reputation · 27 Aug 2020, 13:29 UTC
Yarn Berry (v2+) utilizes the Plug'n'Play (PnP) installation strategy to eliminate node_modules by mapping dependencies via a .pnp.cjs file. While this optimizes installation speed, certain packages containing native binaries or those requiring direct filesystem access for compilation may fail when accessed from within a cached zip archive.
The unplugged folder provides a mechanism to extract these specific packages to the physical disk. However, determining the optimal criteria for automatic unplugging versus manual configuration in .yarnrc.yml remains a point of architectural uncertainty for large-scale projects with diverse native dependencies.
32775 reputation · 27 Aug 2020, 22:46 UTC
What triggers Yarn to move a package to the unplugged directory automatically?
install, preinstall or postinstall script in its package.json will be unplugged. Yarn executes those scripts in a real filesystem context, so the package must be materialised on disk.preferUnplugged: true in its own package.json is also unplugged, regardless of whether it has install scripts.node_modules field or contain native bindings that probe __dirname at runtime may be unplugged, but this is not an automatic trigger; it is recommended to test such packages manually.Is there a configuration property to explicitly define a list of packages that must be unplugged regardless of detected binary presence?
package.json, add a dependenciesMeta section and set unplugged: true for any package you want to force‑unplug.{
"name": "my‑app",
"private": true,
"dependencies": {
"node‑git": "^1.0.0",
"sharp": "^0.32.0"
},
"dependenciesMeta": {
"node-gyp": { "unplugged": true },
"sharp@^0.32.0": { "unplugged": true }
}
}
Yarn will then place node-gyp and sharp under .yarn/unplugged even if it does not detect install scripts or native binaries.
yarn install after editing package.json..yarn/unplugged.
ls .yarn/unplugged/node-gyp should list the real folder.yarn unplug <package> to force‑unplug a single package and confirm it is extracted.node -e "require.resolve('sharp')" to ensure the module resolves to the physical path, not a zip archive.To ensure the guidance applies to your environment, please confirm the Yarn version you are using (e.g., yarn --version). The dependenciesMeta.unplugged flag was introduced in Yarn 3.0, and its exact behaviour may vary slightly in later releases.
preferUnplugged are present.dependenciesMeta in the root package.json to force‑unplug arbitrary packages.yarn install or yarn rebuild to regenerate the .pnp.cjs mapping.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.