Pnpm Principle

Do you need some operating system knowledge to understand how pnpm works

1.The nature of the document

In the operating system, a file is actually a pointer, only instead of pointing to a memory address, it points to an external storage address (in this case, external storage can be a hard disk, a USB stick, or even a network).

When we delete a file, what we are deleting is actually a pointer, so no matter how large the file is deleted, it is very fast. Like our U disk, hard disk files, although it looks like they have been deleted, but in fact, the data recovery company can be recovered, because the data still exists, as long as the file is deleted after no storage of other files can be recovered

2.Copy files

If you copy a file, you copy the content that the file pointer points to, and then create a new file pointing to the new content

The concept of hard linking comes from the Unix operating system, which refers to copying A pointer to file A to another pointer to file B, which is a hard link to file A.

By hard linking, no additional disk footprint is created, and the same disk content is found for both files.

There is no limit to the number of hard links, and multiple hard links can be generated for the same file.

The windows Vista operating system supports the creation of hard links. You can use the following command in cmd to create hard links.

1
mklink /h  link.txt temp\article.txt

Example: Create a hard connection

1.First create a folder temp, and create an article.txt text file in the temp folder

2.Next, I’ll create a hard link in the temp folder root (under the pnpm package manager)

  • Press window+R to bring up the window, run it as administrator, and type cmd and enter
  • Since the pnpm package manager folder is on drive F, first switch to drive F and go to the pnpm package manager address
  • Enter: mklink /h link.txt temp\article.txt Enter
  • At this point, you can see the newly created hard link link.text in the editor, so that ·article.txt and link.txt are the same
  • In this case, when you modify link.txt, article.txt will also change, because they point to the same disk space

Note:

  1. The folder (directory) cannot create a hard link because the folder (directory) does not have the file content
  2. In the windows operating system, you generally do not create hard links across drive letters

Symbolic links are also called soft links. If A symbolic link B is created for A file or folder A, B points to A.

The windows Vista operating system supports the creation of symbolic links. Use the following command in cmd to create symbolic links:

1
2
3
mklink  /d   Link Name  Target File
# /d Indicates the creation of a symbolic link to a directory, if not written, it is a symbolic link to a file

Early windows systems did not support symbolic links, but it did provide a tool called junction to achieve similar functionality.

  1. Hard links can only link to files, while symbolic links can link to directories
  2. The hard link is only associated with the content of the file after the link is complete, and has nothing to do with the previously linked file. Symbolic links are always associated with the previously linked file, and are not directly related to the content of the file

6.Shortcuts

Shortcuts are similar to symbolic links and have been supported by windows since the early days. It is not only a pointer to other files or directories, but also contains a variety of information: permissions, compatibility boot mode and other attributes, because the shortcut is unique to the windows system, it is generally not used in cross-platform applications.

A hard link is an actual file that node doesn’t do anything special to treat differently; in fact, node has no way of knowing whether the file is a hard link or not

Since the symbolic link points to another file or directory, node uses the original path when executing the JS file under the symbolic link. For example: I installed LOL on disk D, created a LOL shortcut on the desktop, which is equivalent to a symbolic link, double-click the shortcut to run the game, and run the game in accordance with the original path of LOL (disk D path).

8.pnpm principle

pnpm uses symbolic and hard links to build the node_modules directory

Here’s an example to illustrate how it’s built

Suppose two packages a and b, with a dependent on b:

Assuming that our project is proj and directly depends on a, pnpm will do the following processing during installation:

  1. Query the dependencies through package.json to get the final packages to install: a and b
  2. Check whether a and b already have caches in the project proj root directory, if not, download them to the cache, if so, go to the next step
  3. Create the node_modules directory in proj and structurally initialize the directory
  4. Use a hard link from the cached corresponding package to place files into the corresponding package code directory
  5. Use symbolic links to place the direct dependencies for each package in its own directory
  6. The purpose of this is to ensure that a’s code can read their immediate dependencies during execution
  7. The new version of pnpm, in order to solve the problem of some ill-written packages (read indirect dependencies), adds all of the project’s non-direct dependencies to.pnpm/node_modules using symbolic links. If b depends on c and a uses c directly, this non-standard usage is now supported by pnpm in this way. But for those who use the absolute path of weird writing, there may be no way to support.
  8. Use symbolic links in the node_modules directory of the project to place direct dependencies