当前位置:网站首页>The difference between NPM and yarn
The difference between NPM and yarn
2022-06-26 10:41:00 【My name is 985】
On Monday , colleagues JJ Let me familiarize myself with the basis of React New projects .
As always , My steps are :
git clone xxx
npm install
npm run dev
At this time ,JJ Here's the next paragraph for me
git clone xxx
yarn
yarn start
“ Why ,yarn What the hell is it ? Don't npm More advanced alternatives ? Why replace npm? Is there any good place ?”, A series of internal problems emerge . I asked in silence JJ:“yarn with npm The same thing ?”,“ Um. .”JJ Busy typing keyboard , Obviously, this question is not worth asking . I also wrote down a series of questions in my mind just now .
Yarn What is it? ?
“Yarn By Facebook、Google、Exponent and Tilde Joint launch of a new JS Package management tools , just as Official documents Written in ,Yarn To make up for npm Some of the defects of .” This sentence reminds me of using npm It's time :
npm installWhen Great slowness . In particular, it will take half a day for new projects to be pulled down , Delete node_modules, again install It's still the case .- The same project , During installation There is no consistency . because package.json The characteristics of the version number in the document , The following three version numbers represent different meanings during installation .
"5.0.3",
"~5.0.3",
"^5.0.3"
“5.0.3” Indicates the installation of the specified 5.0.3 edition ,“~5.0.3” Said the installation 5.0.X The latest version of ,“^5.0.3” Said the installation 5.X.X The latest version of . That's the trouble , Often the same project appears , Some colleagues are OK Of , Some colleagues will appear due to different versions installed bug.
- During installation , Packages will be downloaded and installed at the same time , At some point in the way , A package threw an error , however npm Will continue to download and install packages . because npm All logs will be output to the terminal , Error messages about error packets will be in a lot of npm The printed warning is missing , And you'll always You won't notice what actually happened .
With these holes , I began to understand Yarn The advantages of and the problems to be solved .
Yarn The advantages of ?
- Fast . Speed comes from the following two aspects :
- Parallel installation : No matter what npm still Yarn When performing package installation , Will carry out a series of tasks .npm Is to execute each... In a queue package, That is to say, we have to wait until now package After installation , In order to continue with the later installation . and Yarn It's doing all the tasks simultaneously , Improved performance .
- Offline mode : If a package has been installed before , use Yarn Get from cache between re installation , No need. npm Then download it from the Internet .
- install Unified version : To prevent pulling different versions ,Yarn There is a lock file (lock file) It records the version number of the module installed exactly . Every time you add a module ,Yarn Will be created ( Or update )yarn.lock This file . And that's what guarantees , Every time you pull the same project dependency , The same module version is used .npm In fact, there are ways to use the same version of packages, But it needs to be executed by developers npm shrinkwrap command . This command will generate a lock file , In execution npm install When , The lock file will be read first , and Yarn Read yarn.lock There is a truth in the document .npm and Yarn The difference between the two is ,Yarn By default, a lock file like this is generated , and npm To pass the shrinkwrap Command to generate npm-shrinkwrap.json file , Only when this file exists ,packages The version and information will be recorded .
- More concise output :npm The output information of is rather lengthy . In execution npm install <package> When , The command line will continuously print out all installed dependencies . by comparison ,Yarn Too much simplicity : By default , Combined with the emoji Print out the necessary information intuitively and directly , It also provides commands for developers to query additional installation information .
- Multi registration source processing : All dependency packages , No matter how many times it is indirectly referenced by different libraries , When you install this package , Only from one registered source , Or npm Or bower, To prevent confusion and inconsistency .
- Better semantics : yarn Changed a little bit npm Name of the command , such as yarn add/remove, It feels better than npm The original install/uninstall To be clearer .
Yarn and npm Command comparison
| npm | yarn |
|---|---|
| npm install | yarn |
| npm install react --save | yarn add react |
| npm uninstall react --save | yarn remove react |
| npm install react --save-dev | yarn add react --dev |
| npm update --save | yarn upgrade |
npm The future of :npm5.0
With yarn After the pressure of ,npm Some similar improvements have been made .
- By default, it adds something like yarn.lock Of package-lock.json;
- git Rely on Support Optimization : This feature requires a large number of internal projects to be installed ( For example, in the Intranet development without self built source ), Or when you need to use unpublished versions of certain dependencies . Before that, you may need to specify commit_id To control the version .
- File dependency optimization : Previous versions , If you install a local directory as a dependency , The file directory will be copied as a copy to node_modules in . And in the npm5 in , Will be used instead to create symlinks The way to achieve ( Use local tarball Except for bags ), Instead of copying files . This will speed up the installation . at present yarn Not yet .
summary
stay npm5.0 Before ,yarn The advantages are particularly obvious . But in npm after , Through the above series of comparisons , We can see npm5 There has been a big improvement in speed and usage , It's worth trying , But not more than yarn.
To sum up, my personal advice is if you have already used yarn, And there are no more problems , It can still be used at present . But if there's compatibility npm Scene , Or being in use npm,cnpm,tnpm The team , And not yet cut to yarn Project , Then you can try it now npm5 了 .
边栏推荐
- Oracle11g reports an error when starting the database ora-27154: post/wait create failed
- MySQL项目8总结
- How to start the learning journey of webrtc native cross platform development?
- Reshape a two-dimensional array with 3 rows and 3 columns to find the sum of the diagonals
- MySQL 13th job - transaction management
- String class intern() method and string constant pool
- The IE mode tab of Microsoft edge browser is stuck, which has been fixed by rolling back the update
- Progressive web application PWA is the future of application development
- 2. merge two ordered arrays
- 字符串常量池、class常量池和运行时常量池
猜你喜欢

Servlet learning notes II

Quantitative investment learning - Introduction to classic books

Detailed explanation of winsorflow quantum installation process

Enter a positive integer with no more than 5 digits, and output the last digit in reverse order

36 qtextedit control input multiline text

MySQL 8th job

AdaptiveAvgPool2D 不支持 onnx 导出,自定义一个类代替 AdaptiveAvgPool2D

MySQL 10th job - View

Flutter and native communication (Part 1)

904. fruit baskets
随机推荐
开发者,微服务架构到底是什么?
Flutter and native communication (Part 1)
MySQL Chapter 4 Summary
String class intern() method and string constant pool
Redis中执行Lua脚本
SQL Server 基础介绍整理
Express (I) - easy to get started
How QT uses quazip to compress and decompress files
Searchview click failure
Index summary of blog articles -- Industrial Internet
看我在Map<String, String>集合中,存入Integer类型数据
Which PHP open source works deserve attention
【软件项目管理】期末复习知识点整理
MySQL第八次作业
VS或Qt编译链接过程中出现“无法解析的外部符号”的原因:
Introduction to sysbench Basics
8-图文打造LeeCode算法宝典-最小栈与LRU缓存机制算法题解
Jar version conflict resolution
【无标题】
Renesas electronics launched a complete intelligent sensor solution for Internet of things applications