yarn: Always use `yarn add --link-duplicates`

Viewed 3022

yarn help add says:

--link-duplicates       create hardlinks to the repeated modules in node_modules

I assume this is about hardlinking files since files and modules are in one-to-one correspondence, and linux doesn't support non-root directory hardlinks. I could be wrong though, since the node_modules directory should really be called node_packages.

  1. Is there any reason why I wouldn't want to always use this option to save disk space?

  2. Can I enable --link-duplicates as a default so I don't have to type it out each time?

2 Answers
  1. Node packages may have issues with symlinks, this issue shows that it's unpredictable. If you verified your app and dependencies to be working with --link-duplicates you could enable the option, but it could suddenly fail when you make changes.

  2. yarn config set link-duplicates true [--global], or create a .yarnrc file containing link-duplicates true

  1. Since symlinks are not used, this is mostly a non-issue. But packages that modify their own node-modules do exist, and this has a chance of modifying all other copies (depending on whether the action is in-place). One example is npm itself with the audit thing, although no sane person would causally mix yarn with npm.

  2. The correct name for the flag is --install.link-duplicates. So the command is yarn config set [--global] -- --install.link-duplicates true. Note the -- in the command – it tells yarn to not interpret the install thing as an option.

  3. If you are only looking to save disk space, the correct way is to do nothing if you are using a modern filesystem (BTRFS or XFS on Linux). Yarn already uses fs.copyFile with COPYFILE_FICLONE, and will use that if available. If you are using APFS, NTFS or ReFS, you still need to wait.

Related