Yarn - a fast alternative to npm


If you’re using npm you already know this. When running a npm install on a fresh big project setup you can go and grab a cup of coffee. Or maybe two. I’m working with Yarn (an alternative / replacement to npm) for some while now and it’s running very fast and stable. It arrived in the Gentoo repo a few days before so I decided to write a small blog entry about it.

Problems with npm – slow and unpredictable

Libraries or frameworks with many dependencies needs their dependencies and the deps of the deps and so on. npm downloads everything and your node_modules folder grows and grows while your internet connection is busy. This is very annoying especially if you’re doing some CI and build things within containers from scratch. And it’ll be much more complicated if your CI is sandboxed without network access. You only can move your node_modules folder around but then you have to control the content by a VCS for example. And here’s the next problem. npm will install the packages non-deterministically. This may cause some bugs because the setup on each developers or CI machine is not the same so that’s why the VCSed-package of your node_modules folder is necessary. But because the installation is non-deterministically an update of a single package and it’s dependencies can change many things in your node_modules folder and tracking by a VCS can be inefficient because there are huge changes and they’re hard to review.

Speed up your build process

And here is Yarn! Facebook and other big companies with a huge amount of Node.js-modules within their projects had the same problems in the past and they developed a new tool which covers these problems. Yarn supports npm and Bower repositories and caches modules in a global cache directory for further installations so dependencies are not downloaded twice. And this global cache can be source-controlled for example. After everything is downloaded, Yarn links all the content together within your node_modules folder. Fast, easy and secure. And they’re hosting their own repository

Install Yarn on your local machine

Yarn is available for Linux, Windows and MacOS. It’s available in many Linux repositories like on Gentoo or Arch and there are custom repos for Debian and Fedora / CentOS / RHEL users.

On Gentoo you can compile it via

# emerge yarn

After this you’ve got your new executable in /usr/bin/yarn
On the Yarn website there

Use yarn

When you installed Yarn, you can use user existing package.json setup. It’s very simple to use it – the equivalent command to npm i is

$ yarn

Enter this on your command line within your project directory and this will downloads all the dependencies specified in your package.json. Much faster and way more easier to control. So be sure you grabbed your cup of coffee. Check the documentation and more features at the Yarn website

Categories Web