“This is the 18th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

Soft links and hard links

So let’s say we have a file called Hello

Use ln -s to create a soft link, and use ln to create a hard link.

$ ln -s hello hello-soft
$ ln hello hello-hard

$ ls -lh
total 768
45459612 -rw-r--r--  2 xiange  staff   153K 11 19 17:56 hello
45459612 -rw-r--r--  2 xiange  staff   153K 11 19 17:56 hello-hard
45463415 lrwxr-xr-x  1 xiange  staff     5B 11 19 19:40 hello-soft -> hello
Copy the code

The differences are as follows:

  1. A soft link can be understood as a pointer to a source file, which is a single file, just a few bytes long, and it has its owninode
  2. The hard link and the source file both point to a house address, it shares storage data with the source file, and they both have the sameinode

Why does PNPM save space

It solves the dependency duplication problem caused by NPM/YARN tiling node_modules (doppelgangers)

Suppose there are dependencies:

. ├ ─ ─ package - a │ └ ─ ─ [email protected] ├ ─ ─ package - b │ └ ─ ─ [email protected] ├ ─ ─ package - c │ └ ─ ─ [email protected] └ ─ ─ package - d └ ─ ─ [email protected]Copy the code

In NPM or YARN, [email protected] is inevitably installed multiple times, wasting space and causing many problems.

./node_modules/package-a
./node_modules/package-b
./node_modules/package-c
./node_modules/package-c/node_mdoules/lodash
./node_modules/package-d
./node_modules/package-d/node_mdoules/lodash
Copy the code
graph app(node_modules) ---> A(package-a) app ---> B(package-b) app ---> C(package-c) app ---> D(package-d) app ---> X (" [email protected] ") C - > Y (" [email protected] ") D - > Z (" [email protected] ")

Here’s a graphic from Rush to illustrate the point.

This is a common scenario. In normal projects, some libraries of the same version can be installed seven or eight times, such as postcss, ansi-styles, ansi-regex, and things like braces, which you can search in your yarn.lock/package-lock.json.

In PNPM, the directory structure of NPM/YARN is changed to adopt soft link mode, which avoids doppelgangers problem and saves space.

The resulting node_modules is shown below, which also shows that it solves the ghost dependency problem.

./node_modules/package-a ->. PNPM /[email protected]/node_modules/package-a./node_modules/package-b -> The PNPM/[email protected] / node_modules/package - b. / node_modules/package - c - >. PNPM/[email protected] / node_modules/package - c . / node_modules/package - d - > the PNPM/[email protected] / node_modules/package - d. / node_modules/PNPM/[email protected] , / node_modules/PNPM/[email protected] / node_modules/PNPM/[email protected] , / node_modules/PNPM/[email protected] / node_modules/package - a. / node_modules /. / PNPM [email protected] / node_modules/lodash - > . The PNPM/[email protected] / node_modules/[email protected]. / node_modules/PNPM/[email protected] , / node_modules/PNPM/[email protected] / node_modules/package - b. / node_modules /. / PNPM [email protected] / node_modules/lodash - > . The PNPM/[email protected] / node_modules/[email protected]. / node_modules/PNPM/[email protected] , / node_modules/PNPM/[email protected] / node_modules/package - c. / node_modules /. / PNPM [email protected] / node_modules/lodash - > . The PNPM/[email protected] / node_modules/[email protected]. / node_modules/PNPM/[email protected] , / node_modules/PNPM/[email protected] / node_modules/package - d. / node_modules /. / PNPM [email protected] / node_modules/lodash - > The PNPM/[email protected] / node_modules/[email protected]Copy the code

In this way, relying on soft links can solve the doppelgangers problem, if a project takes up 1000 MB, then using PNPM may only take up 800 MB

However, one of the biggest benefits of PNPM is that if one project takes up 1000 MB and ten projects take up 10,000 MB in the traditional way, then using PNPM may only take up 3000 MB and it benefits from hard linking.

Using the example above, [email protected] and [email protected] generate a hard link to the global directory, and if the new project relies on both, the storage space can be reused.

./node_modules/. PNPM /[email protected] -> hardlink./node_modules/. PNPM /[email protected] -> hardlinkCopy the code