
Yarn is a package management tool for Javascript which mostly used for Node.js applications. Yarn is compatible with npm used for installing, configuring, updating and removing npm packages. It is designed to fix npm problems like speeding up installation process, network connectivity issues, etc. In this tutorial, you will learn how to install Yarn on CentOS 7 machine.
Prerequisites
You should have logged in on CentOS 7 system with non-root user with sudo privileges.
Install Yarn on CentOS
Follow the below steps to install Yarn on CentOS 7 system. At first, check that Node.js is installed or not by typing:
node --version
If not installed then update Nodesource repository using following curl command:
curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
After that, run below command to install Node.js:
sudo yum install nodejs
To install Yarn, you need to add and enable Yarn repository to your system then install Yarn. You can import Yarn repository’s GPG key using below 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
Now you can install Yarn by typing below command:
sudo apt install yarn
Verify the installation of yarn by issuing below command:
yarn --version
It should show like as below:
Output
1.13.0
Use of Yarn
Create a new Project
You can create a new project using yarn init command as given below:
yarn init new_yarn_project
It will be prompted to you few questions. You can provide information if you want otherwise you can skip by just hitting Enter key for all the questions.
Once all the questions are completed then it will create a package.json file with the information which you provided. You also can make changes manually to that file.
Install All Dependencies
To install all project dependencies that are given in the package.json file run:
yarn install
or you can run just yarn as below:
yarn
Adding dependency
When you have requirement to install another package in your project then you need to add it to the project dependencies. Below is the basic syntax to add dependency:
yarn add [package_name]
This command will update the package.json file yarn.lock file. When you want specific version of any package then you can followed by version number as following:
yarn add [package_name]@[version_or_tag]
Upgrading dependency
You can upgrade a dependency using any one of the following command:
yarn upgrade [package_name]
or
yarn upgrade [package_name]@[version_or_tag]
It will update specific package and make changes in package.json and yarn.lock file.
Removing dependency
To remove a dependency using yarn use below command:
yarn remove [package_name]
Same as add and update command this will also update the project’s package.json and yarn.lock files.
Conclusion
Here, we described how you how to install yarn on CentOS 7 system. You can get more details about yarn at Yarn Documentation page.
If you have any query or suggestion you can comment below.
Leave a Reply