How To Install and Use Yarn on Centos 7

In this guide, we want to teach you How To Install and Use Yarn on Centos 7.

Yarn is one of the most popular dependency managers. Developers use it to package projects in a compact format and share them with communities worldwide.

Yarn is known for its high speed, robust security, and consistency. It was created to address the issues with the node package manager (npm), its famous competitor.

Steps To Install and Use Yarn on Centos 7

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide the Initial Server Setup with Centos 7.

Installing Node.js on Centos 7

To install yarn, you must have Node.js installed on your server. First, update your local package index with the following command:

sudo yum update -y

Then, use the command below to enable the Nodesource repository:

curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash -

Next, use the command below to install Node.js on Centos 7:

sudo yum install nodejs -y

Add Yarn GPG Key and Repository

The official Yarn repository is consistently maintained and provides the most up-to-date version. To enable the Yarn repository and import the repository’s GPG key run the following commands:

#curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
Output
[yarn]
name=Yarn Repository
baseurl=https://dl.yarnpkg.com/rpm/
enabled=1
gpgcheck=1
gpgkey=https://dl.yarnpkg.com/rpm/pubkey.gpg
sudo rpm --import https://dl.yarnpkg.com/rpm/pubkey.gpg

Run the system update:

sudo yum update -y

Install Yarn Package Manager on Centos 7

At this point, you can simply install Yarn Package Manager with the command below:

sudo yum install yarn

Verify your yarn installation on Centos 7 by checking its version:

yarn --version
Output
1.22.19

How To Use Yarn Command on Centos 7

At this point, we will show you how to use Yarn by creating a new project and adding/removing dependencies.

To create a new Yarn project, you can use the yarn init command.

Here we create a project named my_project, you can choose another name for it:

yarn init my_project

This command will ask you some questions. Press enter to accept the defaults or fill them with your answers.

This will look like this:

Output
yarn init v1.22.19
question name (root):
question version (1.0.0):
question description:
question entry point (index.js):
question repository url:
question author:
question license (MIT):
question private:
success Saved package.json
Done in 4.61s.

This command will create a basic package.json file that contains the information you have provided.

You can modify this file any time you want.

Also, you can initiate a Yarn project in an existing directory on Centos 7. To do this, switch to the directory and run the following command:

yarn init

To add a package as a dependency to your project you can use the following command:

yarn add [package_name]

With this command, you can install the package and any packages that it depends on. Also, it will update the project’s package.json and yarn.lock files.

If you use only the package name in the command, Yarn will install it in the latest version.

To install a specific version or tag, use the following command:

yarn add [package_name]@[version_or_tag]

Also, you can upgrade the packages with the following commands:

$yarn upgrade
$yarn upgrade [package_name]
$yarn upgrade [package_name]@[version_or_tag]

If no package name is given, the command will update the project dependencies to their latest version according to the version range specified in the package.json file. Otherwise, only the specified packages are updated.

To remove a package from your project dependency you can use the Yarn remove command on Centos 7:

yarn remove [package_name]

This command will also update the project’s package.json and yarn.lock files.

In an existing project you can install all dependencies that are specified in the package.json file with the following command:

yarn

Or you can use:

yarn install

Conclusion

At this point, you have learned to Install Yarn on Centos 7 and basic usage of it.

Hope you enjoy it.

You may be like these articles:

How to Change SSH port in Centos 7

How to install LAMP stack on centos 7

set up Apache virtual host on centos 7

How to install LEMP stack on Centos 7

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!