跪拜 Guibai
← Back to the summary

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:

  1. Directory Entry (the "house number"): A folder at the bottom level is like a "contact list table." The name index.js you see in the GUI only exists in this table, followed by a number (e.g., Inode #8848). The filename itself contains no code content.
  2. Inode (index node, the "property deed"): The OS follows the number 8848 to 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.
  3. 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).

A quick soul-searching question: Why can't you create a hard link to a folder?

Imagine a parent folder /A contains a child folder /A/B. If you were allowed to create a hard link link_to_A in B pointing to A, its Inode would be identical to /A's, and the system would see it as /A itself—so /A contains /B, and /B contains /A, turning the directory tree into an infinite Möbius strip! Not only would find, rm -rf, and packaging scanners plunge into an infinite loop and exhaust the CPU, but even typing cd .. (go to parent) inside /A would 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."

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:

  1. 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 .js file in it to each project's node_modules/.pnpm/ via hard links. 10 projects share the same physical disk space, and deleting one project doesn't affect the global store.
  2. 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 entire react folder. So pnpm cleverly places a folder soft link at the top-level node_modules/react, acting like a signpost to precisely guide Node.js into the deep directory in the .pnpm store.

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):

  1. 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.
  2. Electron desktop app packaging: Tools like electron-builder, when packaging node_modules into .asar archives, often fail to correctly handle cross-directory soft links, leading to white screens or missing dependencies after packaging.
  3. 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.
  4. 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.

Comments

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.