How to Install Yarn on CentOS 7

How to Install Yarn on CentOS 7
How to Install Yarn on CentOS 7

Install Yarn on CentOS 7

Yarn is a advanced package management tool for Javascript applications mostly used for Node.js applications. Yarn is compatible with npm used for installing, configuring, updating and removing npm packages. It helps to solve problems with npm like network connectivity issues, speeding up the installation process etc. In this tutorial we are going to install Yarn on CentOS 7.

Prerequisites

Before you start to install Yarn on CentOS 7. You must have the non-root user account on your system with sudo privileges.

Install Yarn on CentOS 7

First check if Nodejs is installed on your system running following command:

node --version

If Node.js is not installed run following commands to install it:

sudo yum install nodejs

Update Nodesource repository using following curl command:

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

Install Node.js typing following in the terminal:

sudo yum install nodejs

To install Yarn first import the GPG key to enable Yarn repository using curl command:

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

To install Yarn run following command in the terminal and hit ENTER:

sudo yum install yarn

Now confirm installation and check version of Yarn using following command:

yarn --version

The output should look like:

1.13.0

Eaxamples of Using Yarn

Create a new Project

You can create new project using Yarn running following command. This command will also ask you some questions please press ENTER to use default values or type answer.

sudo yarn init new_project

NOTE: Now it will create package.json file saving information provided by you. You can also edit the information inside file anytime.

The above command output should look like:

yarn init v1.13.0
question name (vagrant): Linux4one
question version (1.0.0): 0.0.1
question description: Testing Yarn
question entry point (index.js): 
question repository url: 
question author: Linux4one
question license (MIT): 
question private: 
success Saved package.json
Done in 17.56s.

Install all dependencies

To install all the dependencies given inside package.json file use following command

yarn install

The above command will install all the dependencies given in package.json file.

Add dependency

Following is the basic syntax for adding dependency:

yarn add [package_name]
yarn add [package_name]@[version_number_or_tag_name]

The above command will add dependency to your project updating yarn.lock and package.json files.

Remove dependency

following is the basic syntax for removing dependency:

yarn remove [package_name]

The above command removes specific dependency from your project updating yarn.lock and package.json files.

Update dependency

The basic syntax for upgrading dependency is given below:

yarn upgrade [package_name]
yarn upgrade [package_name]@[version_number_or_tag_name]

The above command will upgrade specific package to latest version in your project then updates yarn.lock and package.json files.

Conclusion

You have successfully learned how to install Yarn on CentOS 7. If you have any queries regarding this then please dont forget to comment below.