当前位置:网站首页>Recognize pnpm: faster, higher performance NPM

Recognize pnpm: faster, higher performance NPM

2022-06-26 06:58:00 Code Taoist

What is? PNPM?

PNPM yes Node.js Used in substitution NPM Another package manager for , representative “Performant NPM”.

PNPM The main purpose of is to keep all packages in one global ( Centralized ) In storage , And use them when other projects need them by creating hard links .

Use PNPM be better than NPM The advantages of

  • Save a lot of disk space .
  • Installing software packages takes more time .
  • It is right. mono Built in support for repositories .

Now? , Let's try to understand PNPM How it actually works .

So , I need two applications to understand PNPM Use the concepts behind the scenario .

These are two applications package.json file :

{
"name": "application1",
"version": "0.0.0",
"private": true,
"main": "app.js",
"scripts": {
  "start": "node ./bin/www"
},
  "dependencies": {
    "cookie-parser": "~1.4.4",
    "csurf": "^1.10.0",
    "debug": "~2.6.9",
    "ejs": "~2.6.1",
    "express": "~4.16.1",
    "express-session": "^1.17.0"
  }
}
{
 “name”: “application2”,
 “version”: “0.0.0”,
 “private”: true,
 “main”: “app.js”,
 “scripts”: {
 “start”: “node ./bin/www”
 },
 “dependencies”: {
   “cookie-parser”: “~1.4.4”,
   “csurf”: “^.10.0”,
   “debug”: “~2.6.9”,
   “powerbi-client”: “^.16.5”,
   “rxjs”: “^.5.3”
 }
}

install

First , I use... By running the following command npm Installed pnpm:

npm install -g pnpm

Be careful : My system has been installed with Nodejs v16.13.2 and NPM v6.28.0.

We can test by running the following instructions PNPM Is the installation successful :

pnpm -v

The above command is used to check which version of pnpm.

PNPM And NPM What's the difference? ?

In order to understand their working differences , I am for npm and pnpm Two directories are created respectively , Both use two sample applications , And use npm and pnpm Install the software packages in different directories .

npm

application 1:

[email protected]:~/npm-demo$ npm install
added 71 packages, and audited 72 packages in 1s
found 0 vulnerabilities

application 2:

[email protected]:~/npm-demo2$ npm install
added 27 packages, and audited 28 packages in 9s
found 0 vulnerabilities

ad locum , As you can see ,npm In the application 1 Installed in 71 A package , In the application 2 Installed in 27 A package , This is very good .

however , If you look at these two applications from the front package.json file , You will find that both have something in common Three bags .

Even if the application 2 Need some installed dependencies ( In the application 1 in ), We can't reuse what has been npm Dependencies installed by another project in .

pnpm

application 1:

[email protected]:~/pnpm-demo$ pnpm install
Packages: +67
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Packages are hard linked from the content-addressable store to the virtual store.
Content-addressable store is at: /home/rach/.pnpm-store/v3
Virtual store is at:             node_modules/.pnpm
Progress: resolved 67, reused 0, downloaded 67, added 67, done
dependencies:
+ cookie-parser 1.4.6
+ csurf 1.11.0
+ debug 2.6.9 (4.3.3 is available)
+ ejs 2.6.2 (3.1.6 is available)
+ express 4.16.4 (4.17.2 is available)
+ express-session 1.17.2

application 2:

[email protected]:~/pnpm-demo2$ pnpm install
Packages: +27
+++++++++++++++++++++++++++
Packages are hard linked from the content-addressable store to the virtual store.
 Content-addressable store is at: /home/rach/.pnpm-store/v3
 Virtual store is at: node_modules/.pnpm
dependencies:
+ cookie-parser 1.4.6
+ csurf 1.11.0
+ debug 2.6.9 (4.3.3 is available)
+ powerbi-client 2.19.1
+ rxjs 6.6.7 (7.5.2 is available)
Progress: resolved 27, reused 18, downloaded 9, added 27, done

From output , We can notice that pnpm In the application 1 Installed in 67 A package , In the application 2 Installed in 27 A package . however , If you look at the last line in the output —— speed of progress , You will find that it reuses what has been done for the application 1 Installed 18 A package .

“ stay pnpm in , If you have installed a package for another project , Then they will always be reused , This saves a lot of disk space , This makes it more than npm faster 、 More efficient .”

This is amazing !

But where are these packages stored ?

Use pnpm install, Especially this line in the output ,“Packages are hardlinked from the content-addressable store to the virtual store”, Shows how it works .

Content addressed storage is a storage mechanism , The data is stored on the hard disk by assigning a permanent location and addressing it with a unique identifier .

In the screenshot above , You can see that a file named “.pnpm-store” Hidden directory for .

Hidden files can be system or application files stored on a hard disk drive with a permanent location , And hidden to prevent any accidental changes .

This hidden directory —— .pnpm-store go by the name of “ Content addressable storage ”, All downloaded dependencies are stored in it .

Whenever you download a dependency ,pnpm First, check whether the dependency exists in this store .

Hard links are just exact copies of the original file , It also points to the hard drive ( Of the original document ) Same position .

If a dependency already exists in this store , be pnpm Retrieve the same content by creating hard links .

Conclusion

By explaining pnpm And npm The difference between , And the use of pnpm Instead of npm The benefits that can be obtained , We are right. pnpm With a simple understanding . You've tried before pnpm Do you ? What do you think of it ? Do you have any idea ?

Link to the original text

原网站

版权声明
本文为[Code Taoist]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202171318130249.html