There is a new kid on the block, and long story short, he is quite AWESOME.
We are talking about YARN, a new package manager that wants to solve a few problems that have become a real nuisance in today’s favorite, the Node package manager – npm.
First of all, let’s talk about speed, more specifically the speed of package installation. npm takes its time, doing it in a queued install fashion, meaning that each dependency is processed one after the other, leading to a long, very very long installation time. (console > npm install, ok now let’s go make ourselves a coffee).
Yarn does it in parallel. The end. 🙂
Yarn uses both the NpmJS registries, as well as the Bower ones, while npm restricts itself to the NpmJS ones.
Finally, yarn uses a flat dependency structure, whilst npm (v2) used a nested structure, only solving it in it’s third major release (v3).
A quick benchmark that I ran was installing the core dependencies of an Angular project.
npm install 41,058 total yarn 23,908 total
That is great, almost half the time!
Both package managers take the package versions from the package.json file. But there is a special case possibility, where two installations (that used npm install and have the same package.json file) on two different machines, could have mismatching installations of packages.
Yarn solves this case by introducing a .lock file, thus getting consistent installations across machines.
Yes, that is right, Yarn offers an offline command option! And it is a real life-saver. There is a catch though, the package (or list of packages) that you are installing in offline mode must’ve been installed before, with an internet connection. Yarn caches all the packages with the specific version, and if it finds a cached version of it, it installs it. This is a great feature, especially for developers (like me) that travel a lot, and might start a new project on the way or need to add a previously used package, from another project, to the current one.
We all know how ugly and verbose a npm install can be. Yarn is not.
He is blunt, goes straight to the point, and above all that, it has emojis.
Win, win and win.
Of course, since there are functional differences between the previously mentioned package managers, there will (most likely) be different commands.
So, let us get straight to business.
In order to install Yarn, you need to use … npm. This is the easiest method, since you should already have npm installed.
npm install -g yarn
If not, there are alternatives. Windows has an installer, of course.
For Linux and macOS, the following:
macOS ----- brew install yarn Linux (Debian / Ubuntu) ----- curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list sudo apt-get update && sudo apt-get install yarn
Starting a new project is easy as 1, 2, 3, by using the init command. It provides you with a CLI that asks you a few questions and it creates the package.json file.
Dependency management – add / upgrade / remove / download all.
/* ADD */ // latest version yarn add angular // specific version yarn add firstname.lastname@example.org // dev dependency yarn add gulp --dev /* UPGRADE */ yarn upgrade angular /* REMOVE */ yarn remove angular /* DOWNLOAD ALL */ yarn install or yarn
Being an avid Angular framework user, I was quite upset when I found out that there was no possibility to use yarn with Angular (there was an issue where it did not recognize the @ from the Angular package names). But as of Angular CLI Beta 31 release, you can! And they even have a command to make yarn the default package manager.
ng set --global packageManager=yarn
Since this project is backed-up by companies like Google and Facebook, we can only expect great things from it and also great support, even if right now it is in it’s early stages (v0.24 ATTOW).
The speed increase, generated by parallel installation, offline installation feature and clean output are just a few things that motivated me to move from eleven characters (npm install) to just four (yarn).