Fork me on GitHub

Ahad Bokhari

Yarn as a Drop-in Substitute to NPM

Edit   ·   355 words

Yarn is a new JavaScript package manager built by Tilde, Facebook, Google and Exponent. As can be read in the documentation, its purpose is to solve a handful of problems that these teams faced with npm. Note: Yarn isn’t intended to replace NPM, only improves upon it. It uses the same package.json file, and saves dependencies to the node_modules/ folder.

A couple of points worth noting about the differences between Yarn and npm:

  • Yarn.lock File: To avoid package version mis-matches, an exact installed version is pinned down in a lock file. Every time a module is added, Yarn creates (or updates) a yarn.lock file. This way you can guarantee another machine installs the exact same package, while still having a range of allowed versions defined in package.json.

    In npm, the npm shrinkwrap command generates a lock file as well, and npm install reads that file before reading package.json, much like how Yarn reads yarn.lock first. The important difference here is that Yarn always creates and updates yarn.lock, while npm doesn’t create one by default and only updates npm-shrinkwrap.json when it exists.

  • Parallel Installation: Whenever npm or Yarn needs to install a package, it carries out a series of tasks. In npm, these tasks are executed per package and sequentially, meaning it will wait for a package to be fully installed before moving on to the next. Yarn executes these tasks in parallel or asynchronously, increasing performance.

  • Clean Output: By default npm is very verbose. For example, it recursively lists all installed packages when running npm install . Yarn on the other hand, isn’t verbose at all. When details can be obtained via other commands, it lists significantly less information with appropriate emojis (unless you’re on Windows).

Yarn leads the way with better defaults than npm, we get a lockfile for free and installing packages is blazing fast and they are automatically stored in package.json. The impact of installing and using Yarn is also minimal. You can try it on just one project, and see if it works for you or not.

In my humble opinion the aformentioned makes Yarn a perfect drop-in substitute for npm.