Three simple tricks to speed up yarn install

Dev productivity and quality of life improvements are a passion of mine. And speeding up working with code is not only fun, but also essential for building products fast. Happier and faster developers equal happier customers, who get their features and bug fixes sooner!

When dealing with npm-based ecosystem and its myriads of packages, dependencies install time, especially in large projects, can be unreasonably long. Below are three simple tricks that can help you shave off a minute or two, or sometimes even reduce your yarn install time by half 😋

Not anymore!

Bloated yarn.lock problem

Imagine the situation: you’re adding @awesome/tools library to your dependencies, which also depends on utils@^1.0.0library, which is its latest version. After installing @awesome/tools you’ll see in your yarn.lock file:

"@awesome/tools@^1.2.3":
version "1.0.0"
dependencies:
"utils" "^1.0.0"
"utils@^1.0.0":
version "1.0.0"

After a few months you want to add another library that depends on those utils, let’s say @simple/button. Utils library released a few bug fixes and features in the meantime, and its latest version is now 1.5.1, and @simple/buttondepends on it. If you just run yarn add @simple/button, then in the yarn.lock you will see this picture:

"@awesome/tools@^1.2.3":
version "1.0.0"
dependencies:
"utils" "^1.0.0"
"@simple/button@4.5.6":
version "1.0.0"
dependencies:
"utils" "^1.5.1"
"utils@^1.0.0":
version "1.0.0"
"utils@^1.5.1":
version "1.5.1"

Even though 1.5.1 and 1.0.0 versions are semver-compatible, yarn will not merge them into one as you’d expect, and you’ll end up with 2 versions of the same utils in the repo.

It gets worse than that. If you have a few different libraries that depend on version 1.0.0 and a few that depend on 1.5.1, yarn will hoist one of those versions to the root of node_modules folder, but another one would have no place to go (only one version can sit at the root), and they will be installed as copies in node_modules folders of the libraries that use them. You’ll end up with this folder structure:

/project
/node_modules
/utils@1.0.0
/@simple/button
/node_modules/utils@1.5.1
/@simple/form
/node_modules/utils@1.5.1
/@simple/select
/node_modules/utils@1.5.1

And although it seems like you only have 2 versions of utils library, in reality, it can be 5–6-infinite-number of its copies living in your project, all of which need to be copied into their place, and all of which will steal your yarn install time.

Solution

"@awesome/tools@^1.2.3":
version "1.0.0"
dependencies:
"utils" "^1.0.0"
"@simple/button@4.5.6":
version "1.0.0"
dependencies:
"utils" "^1.5.1"
"utils@^1.0.0", "utils@^1.5.1":
version "1.5.1"

Just to give you a taste of what’s possible: in one of the projects I was working on the radical deduplication of all dependencies dropped yarn install time from 3 min to ~1.5min. 50%!

The dark side of monorepo and workspaces

/project
/node_modules
/utils@1.0.0
/packages
/one
/node_modules/utils@1.5.1
/two
/node_modules/utils@1.5.1

In a really big project that I was working on, that had more than 600 workspaces, if we would have allowed as little as 10% of the dependencies to go out of sync, yarn install instead of 5 min would take an hour. For 50% it would just throw with “out of memory” error halfway through :)

Solution

Nuke node_modules situation

If only it could be speeded up a little bit, at least by 50%…

Solution

See where this is going? 😉 Instead of rm -rf node_modules && yarn install habit do rm -rf node_modules/.yarn-integrity && yarn install 🙃. It usually halves the time it takes to run yarn install (tested on at least 3 projects of various sizes).

Hope you enjoyed those three little secrets, applied them to your repo and your yarn install is now faster than ever! 🥳

Frontend architect & aficionado, CI/CD and automations enthusiast. Love solving problems and fixing things.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store