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 project 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 few reasons why Facebook decided to develop own package manager. You can see reason on the website if you scroll down the homepage.
Let me list out main points here:
1. Offline Mode: Yarn is able to work in an offline mode. So, Yarn has caching mechanism so dependencies which 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 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 exact 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 npm install and yarn install command and see the difference.
Now let’s get started and see how yarn is working in practical
How to install Yarn?
First of all, Go to terminal and install yarn globally on the system using the following command:
npm install -g yarn
Installation can be done using NPM(node package manager) because the yarn is available as the package. G represents global flag. Installation takes a second to complete.
After installation, first, check the following command which prints the version:
Next is, initialize the new project using yarn and here is the command:
This command initializes the project and process request some information like name of the project, yarn version, description, entry point, git repository and author name etc.
After submitting 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 lock file which is basically your npm shrinkwrap file. Yarn automatically locks down all the time and it is clean and easy to read format file.
How to add dependencies using Yarn?
Now, let’s add dependencies to project and you can use the following command:
For example, let’s add the bootstrap, so here is the command:
yarn add bootstrap
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 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.
Following command is used to remove 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.