Yarn is a relatively new dependency manager, is open source and was created by members of Facebook and Google, its main feature is the speed.

HOW DO YARN WORK?, WHAT MAKES IT SO INTERESTING?

It is very difficult not to compare Yarn with the cli of npm, so let’s look at some features of Yarn:

1. Yarn caches all packets you download for the first time, to avoid downloading them more than once. This makes it possible to make installations even if you do not have an Internet connection. For this, Yarn creates a local folder where it manages the packages we installed.

2. Yarn parallels packet download operations to maximize resource utilization, so installation times are much faster than other dependency managers. Yarn is downloading dependencies in parallel, without waiting for the previous package to finish downloading.

3. Yarn does not stop installation when a dependency request failed. Yarn again makes the necessary requests to have the packages properly installed and is not stopped by a bug, will always continue to install the other dependencies thanks to its parallel download.

WHAT PROBLEMS YARN MEETS FOR A DEVELOPER?

Before the arrival of Yarn, we had many problems to manage the dependencies of a project, because the installation of dependencies was very slow, tedious and also generated many failures, so to carry out the installation of dependencies of a project was A very cumbersome task, often the size of the project was proportional to the problems we had.

Yarn solves all these problems because it offers us efficient dependency management.

HOW DO I INSTALL YARN SINCE NPM?

Very simple. With the following command:

npm install -g yarn

HOW DO I INSTALL YARN IN MACOS AND / OR LINUX?

curl -o- -L https://yarnpkg.com/install.sh | bash

YARN BASIC COMMANDS?

Yarn has many commands, in this article we will see some of the basic commands:

YARN INIT

Using this command Yarn creates a new project.

yarn init

We will ask: the name of our project, version, description, initial file, repository, author and the type of license of our project.

After this our package.json file will be created

YARN ADD

Using this command Yarn adds a dependency to our project.

yarn add Package_name

EXAMPLES OF USE OF YARN ADD

Adding as main dependency the Express package

yarn add express

Adding as a development dependency the Gulp package

yarn add --dev gulp

YARN UPGRADE

Using this command Yarn updates a dependency of our project.

yarn upgrade Package_name

EXAMPLE OF USE OF YARN UPGRADE

Updating the Gulp package

yarn upgrade gulp

YARN REMOVE

Using this command Yarn removes a dependency from our project.

yarn remove Package_name

EXAMPLE OF USE OF YARN REMOVE

Removing the Gulp package as a dependency of our project

yarn remove gulp

YARN INSTALL

With this command Yarn installs all the dependencies of our project.

In order to use this command we must have our package.json file previously created. For example, we can use a current project created with npm and try installing it with Yarn.

yarn install

EXAMPLE OF USE OF YARN INSTALL

Installing

yarn install

You can also use only the Yarn command

yarn

YUNN RUN

Using this command Yarn executes the tasks declared in the ‘scripts’ key of our package.json.

EXAMPLE OF YARN RUN USE

Having the following content in our package.json file:

Package.json:

"scripts": {
	"Task ":" echo 'hello, I am a task executed with Yarn!'"
}

And in our terminal we execute the called task task, with the following command:

yarn run task

And we will see in our terminal the message: “hello, I am a task executed with Yarn!”.

To carry out this magnificent dependency management Yarn creates a file called yarn.lock, which helps you better organize dependencies and their versions, this file is clearly managed by yarn, so do not worry about this :)

Yarn is a very interesting tool to manage our dependencies, I recommend you give it a try and start using it. I am sure you will quickly see the great benefit of using Yarn.

From my little experience with Yarn I also recommend to use it by creating your package.json file using Yarn, to avoid having problems with installations made with npm, since npm complicates a lot of installation and dependencies, so if you try to install a Project that was created with npm, in many cases problems will arise, ideally add the dependencies using the yarn add command, and once they have their new package.json file, everything is going well :)

If you have more interest in yarn, I recommend you to visit their documentation , as Yarn is very complete and has many additional options.

And if you liked the article or have doubts, queries or improvements to the same article, please do not forget to comment or share it on the networks, that way they help us a lot to continue with these articles.

 

Yarn feature speedSOURCES

Yarn. Documentation. [Online]
Available at: https://yarnpkg.com/en/docs/cli/.

Npm. Javascript package manager. [Online]
Available at: https://docs.npmjs.com/cli/npm .

Author

Am horla

Pin It