当前位置:网站首页>Three simple tips for accelerating yarn install
Three simple tips for accelerating yarn install
2022-06-23 15:30:00 【Code Taoist】

Improving the productivity and quality of life of developers is my passion . Accelerating the use of code is not only interesting , It is also essential to build products quickly . Happier and faster developers are equal to happier customers , Because they can get features and bug fixes faster !
Based on npm The ecosystem and its myriad packages , Installation time of the dependency , Especially in large projects , It may be too long . Here are three simple tips , Can help you save oneortwo minutes , Sometimes it can even reduce yarn install Half the time
bloated yarn.lock problem
If your project is in yarn Up operation , Then some of your dependencies are likely to be copied , Even if they satisfy semver Conditions . Even though yarn promise Data deduplication is not required , But it's not true .
Imagine this : You are putting @awesome/tools Add libraries to dependencies , It also depends on [email protected]^1.0.0 library , This is the latest version of it . install @awesome/tools after , You will be in yarn.lock In the file :
"@awesome/[email protected]^1.2.3":
version "1.0.0"
dependencies:
"utils" "^1.0.0"
"[email protected]^1.0.0":
version "1.0.0" After a few months , You want to add another library that depends on these utilities , for instance @simple/button.Utils The library also released some bug fixes and features , The latest version of it is 1.5.1, also @simple/button Depend on it . If you just run yarn add @simple/button, Then you will be in yarn.lock see :
"@awesome/[email protected]^1.2.3":
version "1.0.0"
dependencies:
"utils" "^1.0.0"
"@simple/[email protected]":
version "1.0.0"
dependencies:
"utils" "^1.5.1"
"[email protected]^1.0.0":
version "1.0.0"
"[email protected]^1.5.1":
version "1.5.1" Even if 1.5.1 and 1.0.0 The version is semver Compatible ,yarn Nor will they be merged into one as you would expect , You'll end up in repo Get two identical versions of utils.
If you have several different libraries that depend on 1.0.0, The others depend on 1.5.1,yarn One of the versions will be promoted to the root of the folder node_modules Catalog , But the other will have nowhere to go ( Only one version can be located in the root directory ) , And they will be installed as copies in node_modules Use their libraries in the folder . You will eventually get this folder structure :
/project
/node_modules
/ [email protected]
/@simple/button
/node_modules/ [email protected]
/@simple/form
/node_modules/ [email protected]
/@simple/select
/node_modules/ [email protected] Although it seems that you only have 2 Versions utils library , But actually , It can be an infinite number of copies that exist in your project , All of this needs to be copied to their location , All this will steal your yarn install Time for .
Solution
duplicate removal ! Or manually make sure that all semver Compatible libraries are resolved to one version , Or use yarn-deduplicate And so on . This is what you want yarn.lock:
"@awesome/[email protected]^1.2.3":
version "1.0.0"
dependencies:
"utils" "^1.0.0"
"@simple/[email protected]":
version "1.0.0"
dependencies:
"utils" "^1.5.1"
"[email protected]^1.0.0", "[email protected]^1.5.1":
version "1.5.1" For example , In a project , I am completely deduplicating all dependencies , take yarn install The time from 3 Minutes to 1.5 minute .
monorepo and workspaces The dark side of
If you use monorepo and yarn work area ( or Lerna, It uses the following workspace ) To manage your package , You can easily find that your dependencies in the local package are out of sync with each other . Similar to the above , If one of your packages depends on [email protected]^1.0.0, Other packages depend on [email protected]^1.5.1, And these packages are not deduplicated in the root directory ,yarn Multiple copies of versions that are not promoted to the root directory will be created , And install them in your workspace node_modules In the folder . under these circumstances , You may not only end up in the root node_modules Multiple copies of the same dependency in , And there are also scattered in repo Copy of the same dependency in :
/project
/node_modules
/ [email protected]
/ packages
/one
/node_modules/ [email protected]
/two
/node_modules/ [email protected] In a very large project I'm working on , It has 600 Multiple workspaces , If we allow only 10% Dependencies of are out of sync ,yarn install It will take an hour , instead of 5 minute .
Solution
If you move to yarn2 Or anything based on pnpm It's not a good choice ( They solved this particular problem ), Then the only other option is to keep the dependencies and roots in the workspace package.json Strict synchronization .
Nuke node_modules
For some reason , When updated yarn When something strange happens after a dependency in , The first solution everyone recommends is nuke nodemodules Folder , And execute a new yarn install. This is usually the last suggestion , Because it miraculously fixes 90% The strange situation of . After a while , You will form a habit , Each time you check the main branch and start developing new functionality, you execute `rm -rf nodemodules && yarn install`.
If only I could speed up a little more , At least speed up 50%……
Solution
At your root node_modules There is a secret file in the folder .yarn-integrity,yarn Write down all the information it needs to know about your repository and its installed dependencies . If the contents of this file are similar to repo The situation in does not match ,yarn It will be updated and the installed packages will be refreshed . If the file is missing , It will be based on yarn.lock Generate it , And update the node_modules The content in . So if most of the content is already correct , Then it just replaces the incorrect content .
We didn't execute rm -rf **node_modules** && yarn install, But to perform rm -rf **node_modules/.yarn-integrity** && yarn install, It can be effectively reduced by half for yarn install Time for .
original text :https://adevnadia.medium.com/three-simple-tricks-to-speed-up-yarn-install-9cae57d159db
边栏推荐
- 【Pyside2】 pyside2的窗口在maya置顶(笔记)
- Sectigo(Comodo)证书的由来
- The team of China University of Mines developed an integrated multi-scale deep learning model for RNA methylation site prediction
- A transformer can only convert alternating current. How can I convert direct current?
- mysql主从只同步部分库或表的思路与方法
- 微信小程序引导用户添加小程序动画页
- 【云驻共创】智能供应链计划:提升供应链决策水平,帮助企业运营降本增效
- Important knowledge of golang: detailed explanation of context
- SQL injection vulnerability (principle)
- Print memory station information
猜你喜欢

JS garbage collection

The running rabbit fell

After nine years at the helm, the founding CEO of Allen Institute retired with honor! He predicted that Chinese AI would lead the world

Top 10 purchase, sales and inventory software rankings!

乐高宣布涨价,炒家更嗨皮了
Redis缓存三大异常的处理方案梳理总结

Millions of bonuses are waiting for you to get. The first China Yuan universe innovation and application competition is in hot Recruitment!
详解Redis分布式锁的原理与实现

2021-04-15

volatile~多线程下变量不可见
随机推荐
Analysis of graphical level-1 programming problem of Electronic Society: cat and mouse
Summary of operating system underlying knowledge (interview)
基因检测,如何帮助患者对抗疾病?
Important knowledge of golang: mutex
Sectigo(Comodo)证书的由来
JSR303数据校验
2021-04-15
[普通物理] 光的衍射
This year's cultural entertainers have turned their sidelines into their main business
RF analyzer demo setup
小米为何深陷芯片泥潭?
MySQL高级语句二
Wechat applet guides users to add applet animation page
这届文娱人,将副业做成了主业
百萬獎金等你來拿,首届中國元宇宙創新應用大賽聯合創業黑馬火熱招募中!
Origin of sectigo (Comodo) Certificate
labelme的JSON文件转成COCO数据集格式
Babbitt | metauniverse daily must read: meta, Microsoft and other technology giants set up the metauniverse Standards Forum. Huawei and Alibaba joined. NVIDIA executives said that they welcomed partic
MySQL create and manage tables
idea查看.class文件 idea查看.class文件夹