pnpm install

1. What is pnpm

The modern package management tool pnpm (performant npm), which means high-performance npm,
is derived from npm/yarn, but solves potential bugs inside npm/yarn, and greatly optimizes performance

2. What are the characteristics

(1) fast

The official benchmark  data is like this, but the actual use found that pnpm’s first download package speed is similar to that of yarn, and its advantage is reflected in the second download of the same package faster

(2) Efficient use of disk space
pnpm internally uses a file system based on content addressing to store all files on the disk. The outstanding feature of this file system is that
1. The same package will not be installed repeatedly. When using npm/yarn, if 100 projects depend on lodash, then lodash is likely to be installed 100 times, and this part of the code is written in 100 places on the disk. But when using pnpm, it will only be installed once, and there is only one place to write in the disk, and the hardlink (hard link) will be used directly when used again later. 2.
Even if a package has different versions, pnpm will greatly reuse the code of the previous version . For example, if lodash has 100 files, and one more file is added after the update version, then the disk will not rewrite 101 files, but keep the hardlink of the original 100 files, and only write the new one document

(3)*支持monorepo

随着前端工程的日益复杂,越来越多的项目开始使用 monorepo。之前对于多个项目的管理,我们一般都是使用多个 git 仓库,但 monorepo 的宗旨就是用一个 git 仓库来管理多个子项目,所有的子项目都存放在根目录的packages目录下,那么一个子项目就代表一个package。

3. Principle of pnpm dependency

Problems with npm and yarn installation packages
Before the emergence of pnpm, both npm and yarn adopted a flat packaging strategy in order to improve the reuse rate of packages. The flat installation method will cause a big discrepancy between our node_modules folder and package.json. For example, you install a package express, but there will be many packages under your node_modules. At this time, there will be some problems (1)
Specter
dependencies
from In terms of the current package reference method, we will look for it from the node_modules folder when importing. As shown in the above figure, if we do not have accepts in package.json, we can actually reference it, because she does Existence, at this time we are accessing the undeclared npm package, if one day the express main package no longer depends on accepts, at this time the project will have a problem of missing dependencies. We regard the sub-packages that the main package depends on, which are not declared and used in the project, can be understood as packages carried by the main package, which we call ghost dependencies.
(2) The uncertainty of the package version
is easy to understand. If both the main packages A and B depend on the accepts package, but A depends on [email protected] and B depends on [email protected], then the flat structure under node_modules shows 1.0 or What about 2.0? The current way is to show whoever installed it later. There are not a few problems caused by this uncertainty in development. "Others can solve this problem with this package, but I can't solve it by installing this package", which is often caused by this reason.
(3) Relying on repeated installation
This is also easy to understand. Both AB and B depend on accepts and different versions. No matter which version is upgraded to the top level of node_modules, this package will be installed twice.

4. Installation and use of pnpm

Official website address:https://pnpm.js.org/installation/

// 全局安装
npm install pnpm -g

//查看源
pnpm config get registry 
//切换淘宝源
pnpm config set registry https://registry.npmmirror.com/

// 使用
pnpm install 包 // 安装依赖
pnpm i  包      // 安装依赖
pnpm add 包     // -S  默认写入dependencies
pnpm add -D     // -D devDependencies
pnpm add -g     // 全局安装

// 移除
pnpm remove 包               // 移除包
pnpm remove 包 --global      // 移除全局包

// 更新
pnpm up                      // 更新所有依赖项
pnpm upgrade 包              // 更新包
pnpm upgrade 包 --global     // 更新全局包

Guess you like

Origin blog.csdn.net/qq_45345113/article/details/130680885