Yarn is a dependency manager for javascript that competes with NPM of Node.js.
Yarn is a Javascript dependency manager created by Facebook, Google, Exponent, and Tilde. The main features that make Yarn very popular are the speed with which it processes dependencies, its security, and flexibility.
Yarn’s speed is thanks to the efficient use of the cache that removed the need to download libraries repeatedly. Meanwhile, checksum verifies the integrity of the libraries to ensure Yarn is very secure, leading to reliability.
Now, we will show how to install Yarn on Ubuntu 18.04.
The most efficient way to install Yarn on Ubuntu 18.04 is through its repository. We will be able to update the application along with the rest of the system easily:
To do this, we must access our server running Ubuntu 18.04. If you’re having trouble, check out our PuTTY tutorial.
Run the following command:
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
With the above command, we have added the necessary GPG key to make sure that the downloaded packages are authentic.
Then, we can add the Yarn repository with the following command:
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
Now, all we need to do is refresh the origins of the software and install Yarn on Ubuntu using APT.
sudo apt update
sudo apt install yarn nodejs
Then, to check if the installation was successful, we can output the version that has been installed:
yarn --version
Output:
yarn init v1.15.2
And as we can see, Yarn’s installation has been successful and we’re ready for work.
The basic use of Yarn is quite simple. First of all, we have to start a new project with the following command:
yarn init project_name
Then, we will be asked some questions related to the project that will later create the .json file where the dependencies we set will be. If we want Yarn to use the default information for the file, we can press enter for each question.
To add a dependency to our project, we just use the following command:
yarn add [package]
However, it is also possible to define a specific version of a package or library as a dependency of our project.
yarn add [package]@[version]
To update a package, we use the upgrade directive and the package name. For example:
yarn upgrade [package]
We can also specify the version of the package to be updated:
yarn upgrade [package]@[version]
Also, we can remove a package from the project with the command:
yarn upgrade [package]
Finally, to install all the defined dependencies, the following command is used:
yarn install
Or
yarn
Remember that these dependencies are defined in the packages.json file.
Developers have to use all kinds of tools that help with application development. All this improves workflow and takes up less time. In this Yarn can help a lot with Javascript dependencies of a project. As we have learned in this tutorial, the installation process is simple, same as the basic uses.
On the other hand, it is recommended that you visit Yarn’s website and consult the official documentation. This way you will have more information, and be able to use this very popular tool to its fullest potential.