How to install and use Yarn Node.js Package Manager on CentOS 8

Yarn is the most popular package manager for node JS and also compatible with npm. It provides help to automate the process installation, configuration and easily removes extra npm packages. Yarn is comparatively faster than npm in terms of package installation speed and can simultaneously install various packages. So, it is considered a good choice over npm.

In this article, you will learn how to install Yarn and manage NodeJS dependencies on CentOS 8 using the command line.

First, you will log in as a root user and open the terminal window using Ctrl + Alt + t.

Installation of Yarn on CentOS 8

You need to execute the following steps to install yarn on CentOS 8:

Install node js

You will ensure that the node js package is installed on your system. If it is not installed then use the following command to install on your system:

$ sudo dnf install @nodejs

Install Node.js on CentOS 8 Press ‘y’ to confirm the installation of the package. In a while, a ‘complete’ status will display on the terminal that will notify that node js have been successfully installed on your system.

Proceed with installation

Node.js Installation

Enable Yarn repository

You will enable the Yarn repository on your system and import the GPG key of the repository:

$ curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo

Enable Yarn repository

$ sudo rpm --import https://dl.yarnpkg.com/rpm/pubkey.gpg

The official repository of Yarn provides the latest version.

Install Yarn on CentOS 8

Once the yarn repository is enabled, now it’s time to install yarn on CentOS 8. For this purpose, you will type the following command on the terminal:

$ sudo dnf install yarn

Install Yarn with dnf

You will press ‘y’ to allow download all necessary yarn packages.

Check Yarn version

Once the installation of yarn is completed, now you can verify the installation of the yarn version on your system using the following command:

$ yarn –version

Check the Yarn version

The installed version of yarn will be displayed on the terminal.

Create a new project using yarn

The yarn has been successfully installed on CentOS 8, we will explore more about some useful yarn commands.

The yarn ‘init’ command is used to create a new yarn project. The init command executes with the project name on the terminal.

For example, if you want to create a new yarn project named my_project then you would execute the following command on the terminal:

$ yarn init my_project

Create Yarn project

You can also give any other name to your project as per your requirements. The script will be displayed on the terminal that will ask you several questions. Either you can answer or press enter that will set the default values.

Add or remove dependencies

You can install or add a new package using yarn. Type the following command to create a new package:

$ yarn add [package_name]

You can also remove a package from the yarn project’s dependencies. For this purpose, you will invoke the yarn remove command with the package name:

$ yarn remove [package_name]

You can also install all dependencies of an existing project that are defined in ‘package.json’ file. Use the following command:

$ yarn

Or

$ yarn install

Conclusion

In this article, I have explained the step by step installation of yarn on CentOS 8 and manage project dependencies. I hope this article would help you. Using yarn you can easily deploy projects. In case of any problem, you can give your feedback via comments.