Hello everyone, today from the computer file system’s hard link symbolic-link, understand the front end of the package management tool. Why do we know it from this perspective? Understand what is the use of hard and soft links, can take the following questions to think about

  • What does NPM/YARN Link do
  • Performant NPM why is the PNPM performant

File system: NTFS for Windows 2000 and later and XFS for Linux7 and later

First, concept and practical operation verification

A hard link is one or more file names in a file. Again, linking is nothing more than linking a file name to the node number used by your computer’s file system. Therefore, we can link the same file with multiple file names, which can be in the same directory or different directories.

Soft link files have shortcuts similar to Windows. It’s actually a special file. In symbolic links, a file is actually a text file that contains information about the location of another file.

1. Soft and hard link operation in Linux

The following command is what I do in a Linux virtual machine

  1. Create a testFile file.
  2. The second step to the file input content ‘friends from afar ‘;
  3. Step 3 Use ln to create hardtestFile in the same directory.
  4. The fourth step is to modify the hard-linked file and add ‘Although far will be used ‘;
  5. Step 5 Check that the contents of the source file testFile and the hard-linked file have been synchronized
[wyq@localhost f]$touch testFile // Create testFile file [wyq@localhost f]$echo 'Come from afar '>testfile [wyq@localhost f]$cat Testfile [wyq@localhost f]$ln testFile hardtestFile [wyq@localhost f]$lshardTestFile testfile [wyq@localhost f]$ Echo 'hardtestFile '>> hardtestFile [wyq@localhost f]$cat testfileCopy the code

2. Soft links in Linux

Build on the testFile file already created above

  1. Ln -s testfile softtestfile // ln -s testfile soft link
  2. Example Modify the soft link file content
  3. Echo ‘Happy ‘>> SoftTestFile
[wyq@localhost f]$ln -s testFile softtestFile // Create testFile file [wyq@localhost f]$cat softtestFile [wyq@localhost f]$lshardTestFile testfile [wyq@localhost f]$echo '>> softTestFile [wyq@localhost f]$cat Softtestfile // Changes in testFile contents It's always fun to have friends come from afar and be sure to kill them from afarCopy the code

We’re going through LS-LI

[wyq@localhost f]$ ls -litotal 819446343 -rw-rw-r--. 2 wyq wyq 45 Jan 17 19:37 hardtestfile
19446346 lrwxrwxrwx. 1 wyq wyq  8 Jan 17 19:27 softtestfile -> testfile
19446343 -rw-rw-r--. 2 wyq wyq 45 Jan 17 19:37 testfile
Copy the code

If one of the source files, soft and hard linked files is modified, the rest of the data is also changed

Hard link features

  1. Whether you modify the source file (testfile), or modify the hard link file (hardtestfile), the data in the other file will change
  2. Either the source file or the hard link file is deleted, as long as another file exists, the file inode number is19446343Hard linking does not create new inode information, nor does it change the total number of inodes.
  3. Hard links cannot be established across file systems (partitions) because the inode number is recalculated in different file systems.
  4. Hard links cannot link directories. If hard links are created for a directory, hard links must be created not only for the directory itself, but also for all subfiles in the subdirectory, which is too complicated for current Linux.

The soft link creates its own inode index and block, so the inode number of the soft link is not the same as the inode number of the source file (19446346), and the block of the soft link is not the real data, but the file name and inode number of the source file. So the main difference between soft link and hard link in principle lies in:

Hard links do not create their own inode index and block, but directly point to the inode information and block of the source file, so the inode number of the hard link is the same as that of the source file. The soft link creates its own inode index and block, so the inode number of the soft link is not the same as the inode number of the source file, and the block of the soft link is not the real data, but only the file name and inode number of the source file

3. Soft and hard links in Windows

Mklink can be used, specific instructions are no longer described, can be searched

However, the window system has three concepts of hard-link, Junction, and symbolic-link. You can refer to the reference document 1 at the end of this article

Second, the NPM/Yarn Link

After reading about hard link and Symbolic link, let’s look at what NPM link does. NPM link is a command of NPM that establishes soft links to files (symbolic-link).

Let’s use a scenario to practice, essentially is associated with two folders, such as debugging NPM package or writing webpack loader these scenarios, can use NPM link to do folder association

Complete the following steps

  1. Create a new folder on drive C link(think of it as an NPM package project), and NPM init initializes package.json("name": 'testnpmlink')
  2. Enter the link directory and execute NPM link to generate a global soft link
  3. In the project project (to load using link project) directory, execute NPM link testnpmlink

Step 1, 2- Global soft links

Step 3 results – Installation in the project

The screenshots show that node_modules can be modified and debugged in both the node.js installation directory and the project. Once you understand the concept and characteristics of soft links above, you will know what NPM Link does

Third, according to the Pnpm

Performant NPM why is the Pnpm performant

PNPM internally uses a content-addressable file system to store all files on disk. This file system is great for:

The same package will not be installed twice. With NPM/YARN, if 100 projects depend on LoDash, it is likely that LoDash will be installed 100 times and written in 100 places on disk. However, when using PNPM, it will only be installed once, only one place in the disk to write, and then use hardlink directly.

Even with different versions of a package, PNPM greatly reuses code from previous versions. For example, if loDash has 100 files and one more file is added after the update, instead of rewriting 101 files to disk, hardLink keeps the original 100 files and only writes to that new file.

God three yuan

The summary is fast installation, saving disk space, and security, etc. See this article for details

This article mainly introduces the PNPM connection mode. As mentioned above, PNPM will only be installed once. The following uses hardlink, as shown in the screenshot below

To summarize the above two screenshots, we can conclude that there are two aspects of information

1. CAS(Content-addressable store)

Content addressed storage, which is a way of storing information, retrieving information based on content rather than location.

2. Virtual store

The virtual store, the directory of links to the store, where all direct and indirect dependencies are linked, the.pnpm directory in the project.

After we install Express, we are prompted that the packages have been hardlinked to a virtual storage location from the content-addressed storage address

The content-Addressable store is in my directory

C:/Users/wyq/.pnpm-store/v3

The Virtual store is under node_modules/.pnpm of the actual project

The following screenshot shows the layout of node_modules with symbolic links. Each file in each package in node_modules is a hard link from the content addressable store

Just to mention a point mentioned in the PNPM documentation, one of the official less-recommended solutions for working with PNPM in projects is shamefully hoist option. This creates a flat node_modules structure, similar to the NPM/YARN hoist mechanism.

PNPM has a different way of thinking than NPM/YARN. We are shamefully sorry if we encounter problems that cannot be solved. Another meaning is that I can solve the dependency problem non-flatly, and you can use it againhoistYou are so shameless.

PNPM does not work across multiple drives or file systems due to the nature of hard links.

1. The storage path has been specified

pnpm config set store-dir /path/to/.pnpm-store
Copy the code

If we execute PNPM install on disk C, the PNPM store must be located on disk C. If the PNPM store is located on disk D, all required packages will be copied directly to the project location rather than linked. This severely limits the storage and performance benefits of PNPM.

2. The storage path is not specified

Multiple stores are created (one per drive or file system). If PNPM install is on disk C, the storage will be created under.pnpm-store in the file system root directory of C. If installed and running on disk D, a separate store will be created at.pnpm-store on D. The project will still retain the advantages of PNPM, but there may be redundant packets per drive.

Final important note

get hands dirty! get hands dirty! get hands dirty!

Reference documentation

1. https://docs.microsoft.com/en-us/windows/win32/fileio/hard-links-and-junctions2. https://docs.microsoft.com/en-us/windows/win32/fileio/creating-symbolic-links3. https://pnpm.io/
Copy the code