npm vs. Yarn

NPM vs Yarn - Help me decide


Introduction

Developers are usually caught in a dilemma when choosing between package managers to use in building and managing project dependencies. Two popular package managers in the ecosystem are NPM (Node package manager) and Yarn (package manager created by Facebook). We’ll be looking at these package managers side by side considering the following features:

At the end of this article, you’ll be able to decide which package manager to use when building your project.

Performance

Performance has been a bone of contention when comparing these two stacks, NPM ( Node package manager) and Yarn (dependency manager created by Facebook). It was believed since it’s introduction, that Yarn had better performance as opposed to NPM. In recent times, NPM (version 6.4.1 ) has bridged the gap with Yarn (version 1.9.4) in terms of performance. In this section of this article, we’ll compare the performance of these stacks and also their CLI syntax, offline capabilities and ability to install packages in parallel.

Module installation speed

This is where we’ll put package installation speeds to the test using a benchmark performed by appleboy. In this benchmark, we’ll be performing speed test with the latest version of Node v10.11.0 at the time of this writing. You can perform these tests yourself by cloning this repository. The result of the test is stated in the table below:

test case npm install npm ci yarn
install without cache (without node_modules) 3m40s 3m10s 1m1s
install with cache (without node_modules) 1m1s 18s 2s
install with cache (with node_modules) 54s 21s 2s
install without internet (with node_modules) - - 2s

Yes! You saw that. Yarn installs without internet while NPM doesn’t. With those results, we don’t need a mother confessor to decide which of the package managers are faster.

CLI syntax Let’s examine the CLI syntax provided by NPM and Yarn. Note that some commands appear in both package managers but do not necessarily do the same thing.

Yarn NPM
yarn: This command is used to install all the packages in a package.json file. npm install: This is used by NPM to install packages from the package.json file.
yarn run: This command is used to run script object specified in the package.json file. npm run: npm run is an alias for the command npm run-script, which does the same thing thing as its Yarn equivalent.
yarn add [package]: This command is used to install a package npm install [package]: This installs a package from NPM.
yarn remove [package]: This command is used to remove a package. npm uninstall [package]: This is the NPM equivalent for removing or uninstalling packages.
yarn version: This command is used to update the version of your application using semantic versioning. npm version: This command lists the version of your application, Node, NPM as well as other Node dependencies.
yarn upgrade: This command upgrade all the packages in the package.json file. NPM has an equivalent but is an installed package. [npm-upgrade](https://www.npmjs.com/package/npm-upgrade).
yarn self-update: This command is used to update Yarn to the latest available version. At the time of this writing, this command is not yet available. NPM has no equivalent command as at the time of this writing.

Let’s look at a few of the differences between NPM and Yarn’s CLI syntax in detail:

  • yarn version vs npm version: These commands are the same but yield different outputs. Yarn’s use of the version keyword is in terms of updating the tag of application in semver (semantic versioning) format, whereas NPM displays a list portraying the version of the current project, Node, NPM, and other dependencies.
  • yarn upgrade vs npm-upgrade: The upgrade keyword, updates all the packages specified in the package.json file for Yarn. NPM doesn’t have an equivalent CLI command but has a global utility module which is installed by running npm i -g npm-upgrade, which is used to update all the packages in package.json by running npm-upgrade.

Offline download Yarn’s offline download feature is one cool feature. How does it work? packages installed using Yarn are installed on the user disk. Although many may condemn this practice, it boycotts the overhead of having to send HTTP requests to get packages which have been installed before. That way, without internet, yarn or yarn add [package_name] installs your packages. NPM, on the other, hand needs the internet to make those HTTP packages to get previously installed packages which many people may like because it doesn’t populate their local disk with packages they may only use one-time.

Parallel Installation Parallel installation is another cool feature exhibited by yarn out of the box. Yarn installs packages in a parallel manner. That is if I have to install five packages and package 2 is taking forever to install, Yarn goes over to package 3 or 4 or 5 it basically installs the packages side by side, unlike the serial manner which NPM uses. Parallel installation is one of the reasons why Yarn triumphs in speed battle with NPM.

Security

Yarn uses a checksum to verify the integrity of packages installed before any code is executed. What is a checksum? A checksum basically is a string of characters created by applying a mathematical algorithm to the contents of a file. It is used to check whether or not two files are the same. How does it work? Anytime a package is installed and is about to be executed, an integrity check is done by using the package’s checksum. NPM, on the other hand, performs integrity checks via SHA-1 (Secure Hash Algorithm) specified in the package-lock.json file. An SHA-1 string is appended on each package block in the package-lock.json and integrity check is performed using the integrity key, which looks like:

...

"wordwrap": {
    "version": "0.0.3",
    "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.3.tgz",
    "integrity": "sha1-o9XabNXAvAAI03I0u68b7WMFkQc="
}

...

License checks
Yarn comes with a handy license checker right out of the box, just in case you’ll want to view licenses for your installed packages. The command yarn licenses list will in alphabetical order list all of the packages which were installed by the yarn or yarn install command and also give you the license (as well as URL to the source code) associated with each package. NPM, on the other hand, doesn’t have a handy license checker out of the box. There are a few open source packages which perform this function, one of which is a license-checker by davglass. Let’s examine both commands:

yarn licenses list

yarn licenses v0.14.0
β”œβ”€ abab@1.0.3
β”‚  β”œβ”€ License: ISC
β”‚  └─ URL: git+https://github.com/jsdom/abab.git
β”œβ”€ abbrev@1.0.9
β”‚  β”œβ”€ License: ISC
β”‚  └─ URL: http://github.com/isaacs/abbrev-js
β”œβ”€ acorn-globals@1.0.9
β”‚  β”œβ”€ License: MIT
β”‚  └─ URL: https://github.com/ForbesLindesay/acorn-globals.git
β”œβ”€ acorn@2.7.0
β”‚  β”œβ”€ License: MIT
β”‚  └─ URL: https://github.com/ternjs/acorn.git
β”œβ”€ align-text@0.1.4
β”‚  β”œβ”€ License: MIT
β”‚  └─ URL: git://github.com/jonschlinkert/align-text.git
β”œβ”€ amdefine@1.0.0
β”‚  β”œβ”€ License: BSD-3-Clause AND MIT
β”‚  └─ URL: https://github.com/jrburke/amdefine.git
β”œβ”€ ansi-escapes@1.4.0
β”‚  β”œβ”€ License: MIT
β”‚  └─ URL: https://github.com/sindresorhus/ansi-escapes.git
β”œβ”€ ansi-regex@2.0.0
β”‚  β”œβ”€ License: MIT
β”‚  └─ URL: https://github.com/sindresorhus/ansi-regex.git
...

After installing the license-checker package from GitHub. Run license-checker. PS: Both commands should be run inside the root directory of your project. The output is like so:

β”œβ”€ cli@0.4.3
β”‚  β”œβ”€ repository: http://github.com/chriso/cli
β”‚  └─ licenses: MIT
β”œβ”€ glob@3.1.14
β”‚  β”œβ”€ repository: https://github.com/isaacs/node-glob
β”‚  └─ licenses: UNKNOWN
β”œβ”€ graceful-fs@1.1.14
β”‚  β”œβ”€ repository: https://github.com/isaacs/node-graceful-fs
β”‚  └─ licenses: UNKNOWN
β”œβ”€ inherits@1.0.0
β”‚  β”œβ”€ repository: https://github.com/isaacs/inherits
β”‚  └─ licenses: UNKNOWN
β”œβ”€ jshint@0.9.1
β”‚  └─ licenses: MIT
β”œβ”€ lru-cache@1.0.6
β”‚  β”œβ”€ repository: https://github.com/isaacs/node-lru-cache
β”‚  └─ licenses: MIT
β”œβ”€ lru-cache@2.0.4
β”‚  β”œβ”€ repository: https://github.com/isaacs/node-lru-cache
β”‚  └─ licenses: MIT
β”œβ”€ minimatch@0.0.5
β”‚  β”œβ”€ repository: https://github.com/isaacs/minimatch
β”‚  └─ licenses: MIT
β”œβ”€ minimatch@0.2.9
β”‚  β”œβ”€ repository: https://github.com/isaacs/minimatch
β”‚  └─ licenses: MIT
β”œβ”€ sigmund@1.0.0
β”‚  β”œβ”€ repository: https://github.com/isaacs/sigmund
β”‚  └─ licenses: UNKNOWN
└─ yui-lint@0.1.1
   β”œβ”€ licenses: BSD
      └─ repository: http://github.com/yui/yui-lint

Support

NPM is an open source dependency manager created by Isaac Z. Schlueter to meet up with the shortcomings of similar packages such as PEAR. Yarn on the order hand was developed by Facebook to meet up with the shortcomings of NPM. There’s a lot of developer traffic tending towards Yarn reasons being that Yarn is quite faster than NPM. Like shown above on this page, Yarn which is fairly young has more stars compared to NPM as at the time of this writing. These two packages are now well aware of themselves, with Yarn being able to be installed using NPM. Yarn, on the other hand, can install packages from the package-lock.json by using the [yarn import](https://yarnpkg.com/en/docs/cli/import) command. Which will help migrate projects relying on package-lock.json to Yarn. To know more, you can visit the [yarn-import](https://yarnpkg.com/en/docs/cli/import#toc-motivation) documentation.

From Yarn’s blog:

This feature is one of the first fruits of a continuing collaboration between the maintainers of the two package managers. We feel strongly about the two tools being aware of each other and providing an easy transition path between them…

NPM installs all and every package from npmjs.com which hinders accessibility in the event that one goes down, Yarn, on the other hand, installs from multiple repositories npmjs.com as well as Bower. While this doesn’t guarantee everyday availability but it reduces the probability of unavailability to the barest minimum. In the spirit of open sourcing, Listed below are links to their individual repositories, PRs would be welcomed in any of the repositories.

Ease of use

Ease of use or user-friendliness is the last feature we’ll be looking at in this article. In this section, we’ll be looking at two criteria (CLI and user experience), which we’ll use to examine the user-friendliness of these package managers.

CLI (Command Line Interface) Yes! you read that right. I mean, it’s CLI why would UI be a criterion. Let’s take a look at how the terminal looks like when we’re running a simple npm install **** or yarn command.

npm install

yarn

Both installations were done with the same package.json file. We can see that NPM and Yarn have different CLIs. NPM generates noise during installation which can be silenced by using the -s flag to run it in silent mode.

User experience Both package managers have good user experience, like in the case of initializing a new project directory using yarn init or npm init. Where they both provide an interactive mode helping the user set up a new project. Yarn comes out of the box like stated above in performance section with an upgrade command yarn upgrade which helps to update all the dependencies in the project. This command also has an interactive feature which can be used by running yarn upgrade-interactive [--latest] which outputs like so:

[1/? Choose which packages to update. (Press <space> to select, <a> to toggle all, <i> to inverse selection) devDependencies
❯◯ autoprefixer      6.7.7  ❯  7.0.0          https://github.com/postcss/autoprefixer#readme
 β—― webpack           2.4.1  ❯  2.5.1          https://github.com/webpack/webpack

 dependencies
 β—― bull              2.2.6  ❯  3.0.0-alpha.3  https://github.com/OptimalBits/bull#readme
 β—― fs-extra          3.0.0  ❯  3.0.1          https://github.com/jprichardson/node-fs-extra
 β—― socket.io         1.7.3  ❯  1.7.4          https://github.com/socketio/socket.io#readme
 β—― socket.io-client  1.7.3  ❯  1.7.4          https://github.com/Automattic/socket.io-client#readme

NPM, on the other hand, exhibits this same level of user experience just that this feature doesn’t come out of the box but with a package called npm-upgrade. This package can be installed by running this command npm i -g upgrade. After installing, typing npm-upgrade in your project directory and hitting the Enter key will yield the following output:

npm vs Yarn - Comparison Table

  • 1.03K
  • 7.18K
  • 25.4K
  • -
  • 650
  • 3.11K

What is npm?

npm is the command-line interface to the npm ecosystem. It is battle-tested, surprisingly flexible, and used by hundreds of thousands of JavaScript developers every day.

What is Yarn?

Yarn caches every package it downloads so it never needs to again. It also parallelizes operations to maximize resource utilization so install times are faster than ever.
Why do developers choose npm?
Why do you like npm?

Why do developers choose Yarn?
Why do you like Yarn?

What are the cons of using npm?
Downsides of npm?

What are the cons of using Yarn?
Downsides of Yarn?

Want advice about which of these to choose?Ask the StackShare community!

How much does npm cost?
How much does Yarn cost?
What companies use npm?
3209 companies on StackShare use npm
What companies use Yarn?
703 companies on StackShare use Yarn
What tools integrate with npm?
24 tools on StackShare integrate with npm
What tools integrate with Yarn?
9 tools on StackShare integrate with Yarn

What are some alternatives to npm and Yarn?

  • Bower - A package manager for the web
  • RequireJS - JavaScript file and module loader
  • Browserify - Browser-side require() the node.js way
  • Component - Client package management for building better web applications

See all alternatives to npm

npm On-Call
npm On-Call
Why we created npm Enterprise
Yarn import now uses package-lock.json
Ease the Transition to a Monorepo with Focused Works...
Dependencies Done Right


Interest Over Time