March 15, 2017

Why and How to Migrate from NPM to Yarn

By Viktor Tsimbal
Yarn vs npm

Yarn is a new package manager for node.js. It is a common project developed by such companies as Facebook, Exponent, Google, and Tilde. It is distributed under the BSD license. At the time of writing this post, the current Yarn version is 0.17.10.

The main reason why developers choose to transition to Yarn is its stability. In the case of npm, when we need to deploy the project on different machines, the versions of installed packages can be different. I think that was the reason Yarn appeared in the first place. From the main benefits of Yarn we might put a stress on the following:

  • can install packages from the local cache
  • strongly binds package versions
  • allows parallel packages installation
  • has an active user community

NPM vs Yarn: the Difference

Yarn has a few differences from npm. First of all, Yarn caches all installed packages. Yarn is installing the packages simultaneously, and that is why Yarn is faster than NPM. They both download packages from npm repository. Yarn generates yarn.lock to lock down the versions of package’s dependencies by default. On the contrary, npm for this purpose offers shrinkwrap CLI command.

Why Migrate to Yarn

When Yarn appeared, developers greeted it with optimism. More and more developers are now switching to Yarn. Of course, the main reasons for this migration is again stability, ease of use and relatively few differences from npm in terms of use.

There are separate reasons to use Yarn in small or big projects. Its main advantage is the fact that it helps to avoid any possible problems related to different versions of node.js system modules, on which the project will be mounted.

Any Problems with Yarn? Yes, Unfortunately.

Yarn has been developed just recently. Here are 2 main issues which appeared while I was transferring my project from NPM to Yarn:

  • Problems with installing native modules
  • Yarn doesn’t work with any node.js version older than 5

How to Install Yarn

Yarn offers a few ways of installation: install with the brew, Chocolatey (Windows) or from Linux repositories. In my case, the installation was made from Linux repository.

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

Installing Yarn:

sudo apt-get update && sudo apt-get install yarn

Then we need to remove our node_modules folder and install all packages with Yarn:

yarn install

Yarn uses and stores all the packages that were installed in your local cache. When you are installing the package, Yarn is looking for the package in the local cache, and if the package is not found, then Yarn tries to download it from the Internet.

Difficulties with Yarn and Solutions

The first unpleasant surprise was that Yarn won’t work on node.js version 5.10.1. Based on this, I upgraded my version of node.js to a newer one. Now I am using version 6.3.1. Then I installed all packages with Yarn. However, I had an error with the node-gyp module. The same problem appeared when I was installing node-gyp with NPM. Still, I found the solution – installing node-gyp globally.

Installing Yarn packages:

You can install Yarn packages from the command line:

yarn add packageName

Yarn install package and then update package.json and yarn.lock files. There are many ways to install the packages using Yarn: Installing from

  • repository
  • archive
  • git
  • local cache
  • NPM repository

Conclusion:

Yarn advantages over npm fully compensate for all its defects. Yarn allows deploying projects with more comfort and convenience. In addition, it helps to avoid these unpleasant moments, which occur while using npm.

On the contrary to npm, Yarn offers stability, providing lock down versions of installed packages. The speed of modules installing is higher. It is very important for big projects, which have more dependencies. To sum up, I’d say that Yarn is a great alternative to npm.