The pnpm Link Trap: Hard Links, Symlinks, and Why Your Build Breaks
After being burned by pnpm packaging, I finally understood hard links and soft links thoroughly
Every front-end engineer's hard drive probably houses a money-eating beast: node_modules.
Once, to save my poor 512GB SSD, I listened to rumors and decisively switched from npm to pnpm. Installation speed did take off, and I instantly gained dozens of GB of disk space. Just as I thought I had reached the pinnacle of front-end engineering, something strange happened:
Projects that ran fine locally would throw errors when packaged; even after tearfully writing shamefully-hoist=true in .npmrc, Electron packaging still produced a white screen, and deploying to Serverless directly reported "module not found." At the time I was puzzled, until I completely understood the two OS-level concepts of hard links and symbolic links. Then I slapped my thigh and exclaimed: "The ones stabbing me in the back were all those invisible 'pointers'!"
1. Prelude to solving the case: where does the code in a file actually live?
Many students (including past me) instinctively think index.js itself contains the code. But in the file systems of Linux and macOS, a complete "file" is actually stored in three separate areas at the bottom level:
- Directory Entry (the "house number"): A folder at the bottom level is like a "contact list table." The name
index.jsyou see in the GUI only exists in this table, followed by a number (e.g.,Inode #8848). The filename itself contains no code content. - Inode (index node, the "property deed"): The OS follows the number
8848to find the corresponding Inode. This records the file's creation time, read/write permissions, size, and the physical pointer to where the code is stored on the disk sector. - Data Blocks (the "furniture in the house"): Following the physical pointer in the Inode, you reach the actual content—the real code you wrote like
console.log('hello world').
When you double-click index.js in an editor, the OS silently runs a relay race: look up the directory table to get number 8848 → look up the Inode for 8848 to find the physical address → go to the data block to read the code and show it to you.
Once you understand this "separation of name and substance" mechanism, the nature of hard links and soft links becomes common sense.
1. Hard Link: One house with multiple doors, absolutely equal
A hard link is adding another row in the directory contact list, hanging another house number for the same house (the same Inode).
- Underlying truth: The newly created hard link file (e.g.,
alias.js) has the exact same Inode number as the original file, sharing the same "property deed" and the same "code data blocks." So regardless of which one you open, you see the completeindex.jscode; there is no "original" vs "imitation"—they are completely equal. - Deletion protection: The OS internally has a "reference counter." As long as one door (hard link) is open, the house won't be torn down; only when you delete all house numbers (reference count reaches zero) are the data blocks on the disk truly released.
- Two fatal weaknesses: First, cannot cross partitions/disks (different communities have different house number systems); second, cannot create hard links for folders.
A quick soul-searching question: Why can't you create a hard link to a folder?
Imagine a parent folder
/Acontains a child folder/A/B. If you were allowed to create a hard linklink_to_AinBpointing toA, its Inode would be identical to/A's, and the system would see it as/Aitself—so/Acontains/B, and/Bcontains/A, turning the directory tree into an infinite Möbius strip! Not only wouldfind,rm -rf, and packaging scanners plunge into an infinite loop and exhaust the CPU, but even typingcd ..(go to parent) inside/Awould give the system a mental breakdown: should it send you back to the root/, or into the subdirectory/A/B?
2. Soft Link / Symbolic Link: A signpost pointing to a distant place
A soft link is simply planting a wooden sign by the roadside, with a line of text: "Walk 500 meters ahead, turn right, look for index.js." Its principle is almost identical to a Windows desktop "shortcut."
- Underlying truth: A soft link has its own independent Inode number and a tiny file size (tens of bytes). It doesn't directly connect to code data blocks; it only stores a path string.
- Forehead says "I am a shortcut": Because a soft link has its own independent Inode and its type is explicitly marked as "symbolic link," it can cross partitions and safely link folders—all traversal programs recognize it as a signpost and by default avoid it. Even if "follow mode" is enabled, they can detect loops and stop with an error, fully avoiding the hard link infinite loop disaster.
- Fragile constitution: If the target house is demolished, the signpost still stands there foolishly, but following the signpost leads you to a ruin—that's the "dead link (dangling link)" that terrifies programmers.
| Comparison Dimension | Hard Link | Soft Link / Symbolic Link |
|---|---|---|
| Metaphor | Two house numbers for the same house | A signpost pointing to the house's address |
| Inode number | Exactly the same as the original file | Has its own independent Inode |
| After original file is deleted | Rock solid (code data still exists) | Dies immediately (becomes a dead link) |
| Can it link folders? | No (would cause directory tree and cd .. infinite loops) |
Yes (has independent type marker, tools can recognize and avoid pitfalls) |
| Can it cross disk partitions? | No (Inodes are independent) | Yes |
| Creation command | ln source_file hardlink_name |
ln -s source_file softlink_name |
2. pnpm's clever nesting: Why does it mix both types of links?
After understanding the above low-level logic, you can instantly grasp how imaginative the pnpm architecture is:
- Use "hard links" to freeload disk space: If 10 projects on your computer all use
lodash, pnpm stores only one copy of the physical data blocks in the global Store, then maps each.jsfile in it to each project'snode_modules/.pnpm/via hard links. 10 projects share the same physical disk space, and deleting one project doesn't affect the global store. - Use "soft links" to fool Node.js: Although hard links save space, they can't link folders! And when Node.js executes
import 'react', it looks for the entirereactfolder. So pnpm cleverly places a folder soft link at the top-levelnode_modules/react, acting like a signpost to precisely guide Node.js into the deep directory in the.pnpmstore.
This combination of "hard links to save space + soft links for isolation" works flawlessly in regular web development, until it hits those "old-timers" or special packaging environments that don't recognize soft links...
3. What scenarios require node-linker=hoisted?
When I encountered packaging errors before, my first reaction was to enable shamefully-hoist=true. But later I understood: shamefully-hoist=true just creates soft links at the top level for all dependencies in .pnpm (solving the phantom dependency not found problem), but there are still soft links everywhere!
When you find that even with shamefully-hoist=true the project still errors during packaging or deployment, it's usually the symbolic link (Symlink) causing trouble. At that point you must resort to the ultimate move—configuring node-linker=hoisted in .npmrc (completely abandoning .pnpm virtual storage and soft links, returning to the traditional flattened hard link structure):
- React Native / Expo projects: React Native's bundler Metro, in early and some versions, doesn't support resolving symbolic links, and will directly report "module not found" when encountering the default pnpm structure.
- Electron desktop app packaging: Tools like
electron-builder, when packagingnode_modulesinto.asararchives, often fail to correctly handle cross-directory soft links, leading to white screens or missing dependencies after packaging. - Serverless deployments (Serverless / AWS Lambda) or specific Docker builds: Some deployment scripts use compression/copy commands that don't support following soft links to package
node_modules, and after deployment the soft links become dead links. - Legacy old toolchains: Some ancient webpack plugins or custom build scripts traverse the physical paths of
node_modules, and their logic breaks when encountering the real path (realpath) resolved from a soft link.
4. Conclusion
There's no silver bullet in the tech world. pnpm's default isolated mode uses hard links and soft links to build an elegant dependency isolation wall, but when you're in special battlefields like Electron, React Native, or Serverless, decisively writing node-linker=hoisted isn't a compromise to technology—it's a precise breakthrough after understanding the underlying principles.
After all, a configuration that lets you ship smoothly without debugging bugs at midnight is a good configuration.
Top 2 of 4 from juejin.cn, machine-translated. The original thread is authoritative.
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.