Yarn is a JavaScript package manager compatible with npm that helps you automate the process of installing, updating, configuring, and removing npm packages. It was created to solve a set of problems with the npm such as speeding up the packages installation process by parallelizing operations and reducing errors related to network connectivity. Before all, find a good provider to set up your own Linux VPS.
To let this tutorial work better, please consider the below Prerequisites:
A non-root user with sudo privileges.
Table of Contents
How To Install Yarn On CentOS 7
Yarn is an ultra-fast dependency management application designed to cache the packages it downloads so they never need not be downloaded again. It is a secure and reliable program that parallelizes all operations to boost resource utilization and expedite install times.
Join us with this guide to verify How to Install Yarn on CentOS 7 system from the Yarn RPM package repository. In case you are using the new version, find your guide on How to install Yarn on CentOS 8.
Step 1: Installing Yarn on CentOS
If you already don’t have Node.js installed on your system, enable the Nodesource repository.
curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash -
Install the Node.js package by typing:
sudo yum install nodejs
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 issue the following commands:
curl --silent --location https://dl.yarnpkg.com/rpm/SharePoint | sudo tee /etc/yum.repos.d/yarn.repo
sudo rpm --import https://dl.yarnpkg.com/rpm/pubkey.gpg
Once the repository is added, you can install Yarn, by running:
sudo yum install yarn
Then, verify the installation by printing the Yarn version number:
yarn --version
At the time of writing this article, the latest version of Yarn is version 1.17.3.
Output
1.17.3
Step 2: Using Yarn
Now that you have Yarn installed on your CentOS system, we’ll explore some of the most common Yarn commands.
Creating a new project
To create a new Yarn project use the yarn init command followed by the project name. For example, to create a project named my_project you would type:
yarn init my_project
The script will ask you several questions. You can either answer or press enter to use the default values:
Output
yarn init v1.17.3 question name (vagrant): Linuxize question version (1.0.0): 0.0.1 question description: Testing Yarn question entry point (index.js): question repository url: question author: Linuxize question license (MIT): question private: success Saved package.json Done in 20.18s.
Once completed, the script will create a basic package.json file that contains the information you provided. You can open and edit this file at any time.
Adding Dependency
To upgrade the packages, use one of 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.
Upgrading Dependency
To upgrade a dependency use one of the following:
yarn upgrade [package_name]
yarn upgrade [package_name]@[version_or_tag]
The command above will update the project dependencies to their latest version according to the version range specified in the package.json file.
Removing Dependency
To remove a dependency simply type:
yarn remove [package_name]
This command will also update the project’s package.json and yarn.lock files.
Step 3: Installing All Project Dependencies
Therefore, in this step to install all the dependencies of an existing project that are specified in the package.json file run:
yarn
OR
yarn install
Conclusion
In this article, you have successfully installed Yarn on CentOS 7. Also, you understand the basic Yarn usage commands and it should not be difficult to get started. If you want in-depth details on how to use Yarn, go to the official Yarn documentation web page. Find more related articles if you use another OS or windows.
You are the best. How to use NPM to Install Yarn?
Sine Yarn package is available to install with NPM, you can run the command below to install Yarn globally.
sudo npm install yarn –g
Note: If you remove –g option, you can install the current project only.
very good but unfortunately I did not find a command to check any installed version please?
Sure, please use the following command to the version:
yarn -v
1.22.4
Is using Script to install Yarn, recommended?
Yes, you are right. This is recommended as it downloads the Yarn archive and extracts under the .yarn directory at home. So have a look at the following command if you need to go on this way.
curl -o- -L https://classic.yarnpkg.com/install.sh | bash
I want to Install Yarn using Yum.
You can use the following command to configure yarn official yum repository
curl -sL https://dl.yarnpkg.com/rpm/yarn.repo -o /etc/yum.repos.d/yarn.repo
Do you suggest Yarn or NPM?
Yarn and NPM are so close in competition but Yarn considerably faster, opposite of that, NPM 5 introduced a package-lock, which allows for deterministic dependency installation.