What is YARN?

YARN stands for Yet Another Resource Negotiator. It was designed to manage dependencies, like npm in node.js. Yarn manages your project by keeping track of the packages your project depends on and making sure that you always get the right versions installed when you do an install or upgrade. YARN is mainly a JavaScript package manager, but supports other programming languages too.

Yarn can be an alternative to other popular package managers such as NPM (Node) or NuGet ( .NET ). You can use it for managing front-end resources/dependencies like Bootstrap, jquery, etc. Or for back-end packages like pg, node-postgres, sequelize, knex, or anything that’s not a front-end library/resource.

How Does YARN Work?

YARN uses a JSON file to track project dependencies – package.json. The package requires three fields: name, version, and main. A dependency can be either another independent package or another project in your solution. Yarn uses semantic versioning, so you need to specify the version of your package in the form of 0.1.0, 1.2.3, etc.

Yarn will look at this file and install all required packages listed under devDependencies. It also keeps a list of installed packages that it installs with every new build or run, so you can always be sure that things are working as expected.

Why Should I Use YARN?

Yarn is fast, taking only a fraction of the time to install dependencies compared to other package managers. Yarn caches all packages it installs, which makes it super quick if you need to update some package or list of packages.

It also can shrink your node_modules folder before an installation. This can be done with either yarn clean-for-install or yarn cache. The latter will only remove files not referenced by any of your project’s code, ensuring they are removed without breaking anything.

For individuals concerned with NPM’s speed and dependability, yarn is generally considered a superior choice to NPM. Others may prefer yarn to the new versioning syntax.

This tutorial will explain how to install Yarn on Rocky Linux using the command line environment. After installing Yarn, we will go through the basic commands and options of Yarn.

Updating the System

It is necessary to update the system because without updates, they can be susceptible to security vulnerabilities and other bugs. It is important to continually update the system because you want it to be up-to-date and running smoothly. You don’t want it vulnerable and unstable because that could cost you your information and data.

Run the command below to update the system.

sudo dnf check-update
sudo dnf update -y

Run the command below to reboot your system. Rebooting the system is necessary for a number of reasons. A reboot fixes any problems with your system and ensures that everything runs smoothly and efficiently. Reboots will update not only the kernel and the operating system, but also your system’s hardware driver firmware. Reboots can be done automatically through a service like systemd.

sudo reboot now

Prerequisites

  • A server running Rocky Linux 8 or 9
  • An internet connection and root access to the server

Installing Node.JS and NPM

npm is the recommended and most common installation method for installing Yarn on any Linux system. You can install npm by installing Node.JS. As of this writing, Nodejs 16.x is the current stable release.

First, you’ll need to download the Nodesource script to your system by running the command below. You can replace 16.x in the command below with any Nodejs versions.

curl -sL https://rpm.nodesource.com/setup_16.x | sudo -E bash -

Run the command below to check if the script has been added successful.

sudo dnf repolist

You will get an output like the one below.

<img alt="Show repo list" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/04/echo/word-image-19872-1.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="95" src="data:image/svg xml,” width=”625″>

Next, install Node.JS with the command below.

sudo dnf install -y nodejs

After installation is complete, check that node and npm are installed correctly by running these commands on the terminal.

node -v && npm -v

You will get an output like the one below. As long as you get a version number for both of these, you’re good to go.

<img alt="Install node" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/04/echo/word-image-19872-2.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="50" loading="lazy" src="data:image/svg xml,” width=”639″>

Installing Yarn

Run the command below to install Yarn globally on your system using NPM. We use the -g option in the command to indicate that we want Node.JS globally installed on our system so it is available for any project.

sudo npm install -g yarn

Once the installation is complete, run the command below to check if Yarn was successfully installed.

yarn -v

You will get an output like the one below if everything goes well.

<img alt="Yarn version" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/04/echo/word-image-19872-3.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="50" loading="lazy" src="data:image/svg xml,” width=”638″>

Testing Yarn

This section explains how to use basic Yarn commands and options. We will create a simple project requiring us to add a few dependencies.

As a rule of thumb, we should create a new directory for our project to keep it organized. We’ll create the new directory named app_testing_yarn and move into it using the command below.

mkdir app_testing_yarn && cd app_testing_yarn

Once you are in the project directory, you need to initialize the project with yarn by running the command below so we can install dependencies with Yarn.

yarn init

This command walks you through a series of questions that allows you to configure your project’s coding standards and identify the author. You will be asked a few questions regarding your project. You can press Enter to keep the default option or type in your desired option.

Sample output:

<img alt="Yarn init" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/04/echo/word-image-19872-4.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="207" loading="lazy" src="data:image/svg xml,” width=”636″>

A new package.json file will be created once you’ve finished with the questions. A package.json file is a type of configuration file for Yarn applications. It defines metadata about the application, including dependencies and author information.

Use the ls command to list the contents of our directory, and you will see that a new package.json file has been created in the project directory.

ls

Sample output:

<img alt="package.json" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/04/echo/word-image-19872-5.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="49" loading="lazy" src="data:image/svg xml,” width=”623″>

We want to add dependencies for our project so we can use them in our code. To do this, run the command below, replacing the [package_name] with the name of the package you want to install.

yarn add [package_name]

You can get a list of available Yarn packages on its official website.

For example, we are going to install ExpressJS, which is a web framework. So, after running the command below, the express package will be added to our dependencies in package.json.

yarn add express

You will get an output like the one below if everything goes well.

<img alt="yarn add package" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/04/echo/word-image-19872-6.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="301" loading="lazy" src="data:image/svg xml,” width=”628″>

yarn is a pretty straightforward command. As we said earlier, it installs all the dependencies you want and then saves them in the package.json file.

It does this by fetching packages from npmjs.org and saving them in the project directory for you to use. The command above will download and save express in the project directory.

To see all the installed packages, use the yarn list command.

yarn list

As you can see in the screenshot below, only the express package is installed because we didn’t install any other dependencies for this example. However, when you use a real project, you will see all the packages that are installed with Yarn.

<img alt="List packages with yarn" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/04/echo/word-image-19872-7.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="275" loading="lazy" src="data:image/svg xml,” width=”635″>

If you want to upgrade a specific package, use the command below and replace [package_name] with the package name. In this example, we are going to use express as an example.

yarn upgrade [package_name]

yarn upgrade express

Sample output:

<img alt="upgrade package with yarn" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/04/echo/word-image-19872-8.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="286" loading="lazy" src="data:image/svg xml,” width=”632″>

You can also remove a dependency you longer need using yarn. To do this, run the command below, replacing [package_name] with the name of the package you want to remove.

yarn remove [package_name]

For example, we will remove the express package we added in the previous section. After running the command below, the express package will be removed from our dependencies in package.json.

yarn remove express

Sample output:

<img alt="Remove package using yarn" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/04/echo/word-image-19872-9.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="130" loading="lazy" src="data:image/svg xml,” width=”629″>

Conclusion

This tutorial teaches you how to install Yarn on your system. Even though you can use NPM to install all types of packages, using Yarn for your projects is recommended because it provides an easier way to manage dependencies in different JavaScript files for React, Angular, and any other front-end framework. It also makes the dependency management process faster.