当前位置:网站首页>[node] differences among NPM, yarn and pnpm
[node] differences among NPM, yarn and pnpm
2022-07-05 07:10:00 【Giggling Miaomiao house】
npm、yarn、pnpm difference
npm
npm yes Node.js One of the main reasons for being so successful .
npm It is around semantic version control (semver) Designed with the idea of , Given a version number : The major version number . Sub version number . Patch version number , In the following three cases, you need to increase the corresponding version number :
- The major version number : When API Change , And incompatible with previous versions ;
- Sub version number : When the function is added , But when it comes to backward compatibility ;
- Patch version number : As a time to fix backward compatible defects ;
npm Use a name called package.json The file of , The user can go through npm install --save The command saves all the dependencies in the project in this file .
for example , function npm install --save lodash The following lines will be added to package.json In file .
"dependencies": {
"lodash": "^4.17.4"
}
In version number lodash There was a
^character . This character tells npm, Install a major version equal to 4 Any version of . So if I run now npm Installation ,npm Will install lodash The major version of is 4 The latest edition , May be [email protected](@ yes npm The contract is used to determine the specified version of the package name );
majority npm Libraries are heavily dependent on other npm library , This leads to nested dependencies , And increase the probability of not matching the corresponding version .
Although it can pass npm config set save-exact true The close command is used before the version number ^ Default behavior of , But this will only affect top-level dependencies . Because each dependent library has its own package.json file , In front of their own dependencies, there may be ^ Symbol , So I can't get through package.json File provides assurance for nested dependent content .
To solve this problem ,npm Provides shrinkwrap command . This command will generate a npm-shrinkwrap.json file , Record the exact version for all libraries and all nested dependent Libraries .
However , Even exist npm-shrinkwrap.json This file ,npm Only the version of the library is locked , Not the contents of the library . Even if npm It can also prevent users from publishing the same version of the library multiple times , however npm Administrators still have the authority to force updates to certain Libraries .
npm 2 Will install all the dependencies that each package depends on . If we have such a project , It depends on the project A, project A Dependent projects B, project B Dependent projects C, Then the dependency tree will be as follows :
node_modules
- package-A
-- node_modules
--- package-B
----- node_modules
------ package-C
-------- some-really-really-really-long-file-name-in-package-c.js
This structure can be very long . This is based on Unix It's just a little annoyance for our operating system , But for the Windows It's a destructive thing to say , Because there are many programs that can't handle more than 260 Character file pathname .
npm 3 Adopted Flat dependency tree To solve this problem , So our 3 The project structure now looks like this :
node_modules
- package-A
- package-B
- package-C
-- some-file-name-in-package-c.js
such , A long file pathname starts from ./node_modules/package-A/node_modules/package-B/node-modules/some-file-name-in-package-c.js Turned into /node_modules/some-file-name-in-package-c.js.
The disadvantage of this method is ,npm You must first traverse all project dependencies , And then decide how to make a flat node_modules Directory structure .npm A complete dependency tree must be built for all used modules , It's a time-consuming operation , yes npm A very important reason for the slow installation speed .
npm There is a local cache , It saves the compressed package of each version that has been downloaded . Locally cached content can be accessed through npm cache ls Command to view . The design of local cache helps to reduce installation time .
yarn
yarn The main goal at the beginning is to solve npm Caused by semantic version control described in npm The uncertainty of installation . Although it can be used npm shrinkwrap To implement a predictable dependency tree , But it's not the default option , It's up to all developers to know and enable this option .
yarn Different approaches have been taken . Every yarn The installation will generate a file similar to npm-shrinkwrap.json Of yarn.lock file , And it's created by default . In addition to general information ,yarn.lock The file also contains the checksums of the content to be installed , To make sure that you are using the same version of the library .
because yarn It's brand new and redesigned npm client , It allows developers to parallelize all necessary operations , And added some other improvements , This makes the running speed significantly improved , The entire installation time also becomes less .
image npm equally ,yarn Use local cache . And npm The difference is ,yarn Installing locally cached dependencies without an Internet connection , It provides offline mode .
yarn There are also some other improvements , for example , It allows you to merge the licenses of all packages used in the project .
The easiest way to get started is to run :
npm install -g yarn
yarn
but yarn The installation page says so :
Be careful : Generally, it is not recommended to pass npm Installation .npm Installation is non deterministic , The package is not signed , also npm In addition to doing the basic SHA1 No integrity checks are performed outside the hash , This brings security risks to the installation of system programs .
pnpm
pnpm It runs very fast , Even more than npm and yarn.
Because it uses a clever method , Use hard links and symbolic links to avoid copying all local cache source files , This is a yarn One of the biggest performance weaknesses of .
Besides , By 2017 year 3 month , It inherited yarn All the advantages of , Including offline mode and deterministic installation .
summary
yarn Is a safer choice , however pnpm It may be a better choice for some test cases . for example , It can play a role in small and medium-sized teams that run a large number of integration tests and want to install dependencies as quickly as possible .
Last ,npm It still provides a very useful solution , Support a large number of test cases . Most developers use raw npm The client can still do well .
边栏推荐
- [framework] multi learner
- Preemption of CFS scheduling
- SOC_SD_CMD_FSM
- ROS2——配置开发环境(五)
- An article was opened to test the real situation of outsourcing companies
- 扫盲-以太网MII接口类型大全-MII、RMII、SMII、GMII、RGMII、SGMII、XGMII、XAUI、RXAUI
- C#学习笔记
- Mipi interface, DVP interface and CSI interface of camera
- [MySQL 8.0 does not support capitalization of table names - corresponding scheme]
- PostMessage communication
猜你喜欢
![[MySQL 8.0 does not support capitalization of table names - corresponding scheme]](/img/ea/a1e0722c43f56aff3e79f95c99ba8a.png)
[MySQL 8.0 does not support capitalization of table names - corresponding scheme]

逻辑结构与物理结构

Orin 安装CUDA环境

Volcano 资源预留特性

Marvell 88e1515 PHY loopback mode test

ROS2——功能包(六)

docker安装mysql并使用navicat连接

Concurrent programming - deadlock troubleshooting and handling

ROS2——topic话题(八)

. Net core stepping on the pit practice
随机推荐
Ret2xx---- common CTF template proposition in PWN
Xavier CPU & GPU high load power consumption test
[MySQL 8.0 does not support capitalization of table names - corresponding scheme]
iNFTnews | 喝茶送虚拟股票?浅析奈雪的茶“发币”
cgroup_ memcg
Orin 两种刷机方式
全局变量和静态变量的初始化
[OBS] x264 Code: "buffer_size“
. Net core stepping on the pit practice
【软件测试】05 -- 软件测试的原则
小米笔试真题一
[software testing] 05 -- principles of software testing
SD_CMD_RECEIVE_SHIFT_REGISTER
Binary search (half search)
程序中的负数存储及类型转换
Technical conference arrangement
Unity 之 ExecuteAlways正在取代ExecuteInEditMode
Initialization of global and static variables
1290_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
PHY驱动调试之 --- MDIO/MDC接口22号和45号条款(一)