Yarn Vs NPM

Jacky Kimani
Jacky Kimani
Published in
4 min readJun 13, 2017

Introduction

If you haven’t used yarn before but have heard a little about it, or you’re just looking for information about yarn, you’re in the right place!

First off, what is yarn?

Yarn is simply a package manager. It helps you install, upgrade, configure and remove packages from your system, just like npm.

Yarn also enables developers to use and share code with other developers quickly, securely, and reliably.

Note: This article was written using npm version 4

Installing Yarn

For macOS, install yarn using Homebrew. By running the command below you’ll be able to install yarn and also install node incase it’s not already installed

brew install yarn

For Windows, install yarn by either downloading the installer here which will give you a .msi file which will help you install yarn or you can use Chocolatey, a package manager for Windows, similar to Homebrew, by running the following command:

choco install yarn

For Linux, simply run the following command:

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

Alternatively, install yarn by using npm, if you already have it installed. This can be done by running:

npm install — global yarn

To confirm that yarn has been installed run either of the following commands and ensure that the result is the yarn version number in your system:

yarn — versionyarn -V

Usage

Using yarn is very similar to using npm. They both have similar capabilities that are achieved by running similar or different commands. Let us look at some of the commands and see if they are similar or different.

We’ll also be comparing the time each package manager takes to run a process. For us to see the time it takes for npm to complete a task, I’ll be adding `time` before the mentioned command.

Starting a new project

Using npm, you can run the following command to start a new project, or in other words, to initialize a package.json file in your project:

npm init

Similarly, you can start a new project using yarn by running:

yarn init

Once you run the commands, enter details pertaining to the project as preferred.

Adding a dependency

I’ll be using once to illustrate how npm and yarn work. Once is a simple and light node package that is used to run a function exactly one time. I’ll be adding the version 2.0.0 to allow us to upgrade it in the next stage.

To add the dependency using npm you need to run the following command:

npm install once@2.0.0 — save

This took me 1.763 seconds to complete

However, using yarn to install a package and add it to the package.json file, you only need to run the following command:

yarn add once@2.0.0

It installs faster than npm and took me 1.14 seconds to complete

Upgrading a dependency

To upgrade a dependency using npm, you need to run the following command:

npm update once — save

This took me 1.701 seconds to complete

Using yarn, you can upgrade your packages by running the command:

yarn upgrade once

It took me 1.60 seconds to complete which is faster than npm.

Removing a dependency

To remove packages from your system using npm and from your package.json too, run the following command,

npm uninstall once — save

This took me 0.623 seconds to complete

While in yarn, you run the following which approximately takes 0.31 seconds to complete:

yarn remove once

Installing all dependencies of a project

To install all dependencies from a package.json file on a project using npm, run the following which took me 6.927 seconds to complete:

npm install

This is similar to installing the dependencies using yarn whereby you can use either of these two commands:

yarn installyarn

This took me 2.94 seconds to complete and is significantly faster than npm

Yarn Lock File

This is a file that yarn uses to store information about each dependency installed including the version number of the dependencies. This is so that you have consistent installs across different machines.

Major Yarn Features

1. Faster speeds — Yarn parallelizes operations to maximize resource utilization so an operation does not need to wait for another to end before it starts. This makes it faster than npm as shown on the operations above.

2. Offline mode — You don’t need to be online to install a dependency that was previously installed. This is also available in npm version 5

3. Consistency — Dependencies installed on one machine will be installed exactly the same way in another machine regardless of the install order

4. Network resilience — Incase an install fails due to network issues, the request will be retried upon failure.

5. Flat mode — mismatched versions of dependencies are resolved to a single version to avoid creating duplicates.

6. More emojis!! — Cool, right?!

Conclusion

Yarn has some advantages over npm that prove it to be an upgrade from npm. Comparing the speeds of yarn and npm gives us the following values whose differences increase when dealing with more than one dependency and dependencies that are bigger in size:

Yarn is definitely faster, more secure and reliable when handling project dependencies and the extra features make it better to use. It’s easier to use too, proof being the simple commands. If you haven’t given it a try, what are you waiting for?

--

--

Jacky Kimani
Jacky Kimani

Software Developer at Andela && Editor at DevC Nairobi