跪拜 Guibai
← All articles
Frontend · Electron

The pnpm Link Trap: Hard Links, Symlinks, and Why Your Build Breaks

By 去伪存真 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

If your pnpm-based project suddenly fails in Electron, React Native, or a Serverless deploy with "module not found" or a white screen, the cause is likely symlink packaging, not your code. One line in .npmrc flips the layout and fixes it.

Summary

pnpm's disk-saving trick is a two-layer link scheme. It stores one copy of everything in a global store, then hard-links individual .js files into each project's .pnpm directory. Since hard links can't point to folders, pnpm drops a directory symlink at node_modules/react to redirect Node.js. That works for ordinary web apps, but anything that walks node_modules without following symlinks hits a wall.

React Native's Metro bundler, electron-builder when packing .asar archives, and Serverless deployment scripts that use simple copy/zip commands all choke on the symlinks. The common workaround, shamefully-hoist=true, doesn't help because it just adds more symlinks. The real switch is node-linker=hoisted, which abandons the virtual store and flattens node_modules back to a traditional layout.

This is an OS fundamentals story: hard links share an inode and die when the last reference goes; symlinks are paths that go stale. pnpm's design leans on both, and knowing which tool expects what saves you a day of debugging.

Takeaways
— pnpm stores one copy of each dependency in a global store and uses hard links for the .js files.
— Directory symlinks at node_modules/react point into the .pnpm store so Node.js resolves folder imports.
— Hard links share an inode and cannot cross partitions or reference directories; symlinks have their own inode, can cross partitions, and can link folders.
— Setting shamefully-hoist=true only creates more top-level symlinks; it does not remove symlinks from the dependency tree.
— node-linker=hoisted flattens node_modules and eliminates symlinks, restoring the conventional npm layout.
— React Native's Metro, electron-builder's .asar packing, Serverless deployment scripts, and legacy webpack plugins fail to follow symlinks.
Conclusions

pnpm's space savings come at the cost of a nonstandard node_modules layout; the breakage shows up exactly where tools don't honor symlinks.

The common fix, shamefully-hoist=true, is a red herring for symlink issues because it adds symlinks rather than removing them.

Going back to a flattened layout via node-linker=hoisted trades disk space and isolation for compatibility, a reasonable trade only when the target toolchain demands it.

Concepts & terms
Inode
A filesystem structure holding a file's metadata and pointers to its data blocks; directory entries map names to inode numbers.
Hard link
A directory entry that points to the same inode as another entry, sharing the same data blocks. Deleting one name leaves the data alive until all names are gone; can't span filesystems or point to directories.
Symbolic link (symlink)
A small file containing a path string to another target. It has its own inode, can point to files or directories, and can cross filesystems, but goes stale if the target disappears.
node-linker=hoisted
A pnpm .npmrc setting that disables the .pnpm virtual store and symlink layout, producing a flat node_modules like npm's, which avoids symlink-related packaging failures.
From the discussion

The thread offers one practical mitigation: run a dependency-loading test on the packaged output. A follow-up asks what to do when that test finds a problem, a question the article doesn't address. The rest is agreement with the core explanation, especially the hard-link/symlink distinction and the React Native and Windows failure modes.

— A post-build dependency-loading test can catch symlink-related breakage, but the follow-up question exposes that it only detects the failure; the actual fix (such as node-linker=hoisted) still needs to be applied.
— The article's breakdown of hard links versus symlinks and its attention to React Native and Windows specific pitfalls resonate because they map directly to real, painful failures.
Featured comments
卷土的土

Run a dependency-loading test after packaging.

去伪存真

What do you do if the test finds a problem?

组合搭配函数

Finally someone explained the hard-link and symlink thing clearly. Once directory entries and data blocks are separated, pnpm's whole approach immediately makes sense. The points about React Native and Windows pitfalls are well aimed; after reading, I feel confident.

去伪存真

People who've encountered it will remember it more deeply after reading.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗