Yarn is a JavaScript package manager, which is compatible with npm and can help you automatically handle installation, upgrade, configuration, and removal of npm packages.
It was created to solve a series of problems with npm, such as increasing the processing speed of package installation and reducing network connection errors through parallel operations.
This guide will guide you through the installation of Yarn on CentOS 8. We will talk about how to use Yarn to create a new project, and add and remove dependencies.
Perform the following steps as root or another sudo user on CentOS 8 to install Yarn:
sudo dnf install @nodejs
At the time of writing this article, the Node.js version in the CentOS8 software source is v10.x.
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
The official Yarn software source has been maintained and the latest version is available.
sudo dnf install yarn
yarn --version
As of this writing, the latest version of Yarn is 1.21.1
:
1.21.1
Now that you have installed Yarn on your CentOS system, we will explore the most commonly used Yarn commands.
To create a new Yarn project, use the yarn init
command and add the project name. For example, to create a project named my_project
, you can enter:
yarn init my_project
This script will ask you a few questions. You can answer, or press Enter to use the default value:
yarn init v1.21.1
question name(alex): 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 in20.18s.
All the commands just create a basic package.json
file, which contains the information you provide. This file can be modified at any time.
You can also add Yarn in an existing code directory. To do this, switch to the directory and execute:
yarn init my_project
To add a package as a dependency to your project, use the yarn add
command to add the package name:
yarn add [package_name]
This command will install this package and any packages it depends on, and update the project's package.json
and yarn.lock
files.
By default, if only the package name is provided, Yarn will install the latest version. To install a package of a specified version or label, use the following syntax:
yarn add [package_name]@[version_or_tag]
To upgrade the package, run the yarn upgrade
command and add the package name:
yarn upgrade [package_name]
The above command updates all dependencies of the project to the latest version based on the content of the package.json file.
You can specify the package version or label:
yarn upgrade [package_name]@[version_or_tag]
To remove a package from the project dependencies, run the yarn remove
command and add the package name:
yarn remove [package_name]
This command also updates the package.json
and yarn.lock
files of the project.
To install all dependent packages according to the package.json
file in an existing project, run:
yarn
or
yarn install
We have shown you how to install yarn on a CentOS 8 machine. For more information about yarn, browse Yarn Documentation Page.
Recommended Posts