Back to Top

Introduction to Yarn Package Manager

Today in this article, I am going to share about new package manager called Yarn. First I will show to set up the yarn and how to use it in practical project setup.

If you are developing backend and frontend project with node js then you used to using npm to install all your javascript or other third party libraries. NPM is great but there are few deficiencies that yarn can fix it.

What is Yarn?

The website available for Yarn is yarnpkg.com and the first thing you need to know about yarn is, it was initiated by Facebook and supported by Google. The yarn is mainly the replacement for package managers in web development projects like NPM and Bower. The yarn is fully compatible with the npm.

The yarn is also creating a package.json file like npm. It is a configuration file in your project where all your dependencies. The yarn is fully backward-compatible to NPM.

There are a few reasons why Facebook decided to develop its own package manager. You can see the reason on the website if you scroll down the homepage.

Let me list out the main points here:

1. Offline Mode: Yarn is able to work in an offline mode. So, Yarn has a caching mechanism so dependencies that are loaded once are first loaded in the yarn cache and then they are available and if they are requested next time, the yarn can answer the request from the cache.

2. Deterministic: Another important feature of yarn is running an installation in a deterministic mode. It means the structure of the node modules folder installation is exactly the same on every machine where yarn is executed. So, you will have a problem occur due to an installation of a different structure which is often the case in npm. Especially when you are dealing the projects dependent on a lot of different packages.

3. Flat mode: Non nested hierarchical structure of directories

4.Speed: Yarn is much more faster than the npm. You can try the npm install and yarn install commands and see the difference.

Now let’s get started and see how Yarn works in practical

How to install Yarn?

First of all, Go to the terminal and install yarn globally on the system using the following command:

Installation can be done using NPM(node package manager) because the yarn is available as the package. G represents the global flag. Installation takes a second to complete.

After installation, first, check the following command which prints the version:

Next is, to initialize the new project using yarn, and here is the command:

This command initializes the project and process request for some information like the name of the project, yarn version, description, entry point, git repository, and author name, etc.

After submitting the above information, it will generate the package.json. If you see inside the directory, you can see the package.json file

What is Yarn Lock file?

The yarn has a lock file which is basically your npm shrinkwrap file. Yarn automatically locks down all the time and it is a clean and easy-to-read format file.

How to add dependencies using Yarn?

Now, let’s add dependencies to a project and you can use the following command:

For example, let’s add the bootstrap, so here is the command:

Yarn will install the package in a second. If you add the first package, it will give you a message that “no lock file found” because the yarn is working on the lock file, and every dependency, like remove, update or add will be written to the lock file. It will install the latest stable version of the bootstrap.

The following command is used to remove a package

I hope you found this article helpful. The yarn is definitely been a great addition to my project and I hope it helps you out in day-to-day workflows.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

The Readers’ Poll – June 2013

Posted on 10 years ago

Bhumi

Learn about InnoDB MySQL Storage Engine

Posted on 12 years ago

Bhumi

The Readers’ Poll – January 2013

Posted on 11 years ago

Bhumi