In this guide, we want to teach you How To Install and Use Yarn on Rocky Linux 8.
Steps To Install and Use Yarn on Rocky Linux 8
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 on Initial Server Setup with Rocky Linux 8.
Install Node.js on Rocky Linux 8
To install Yarn, you must have Node.js installed on your server.
First, update your local package index with the command below:
sudo dnf update
Then, use the command below to install Node.js on your server:
sudo dnf install @nodejs
You can verify your Node installation by checking its version:
Your Node.js version may be different from here.
Install Yarn on Rocky Linux 8
To install Yarn, you need to import the Yarn GPG key and add its repository to your Rocky Linux server.
Import Yarn GPG Key
You can import the Yarn GPG key by using the command below:
sudo rpm --import https://dl.yarnpkg.com/rpm/pubkey.gpg
Add Yarn Repository
Now use the following command to add the Yarn repository:
curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
Next, update your system repository:
sudo dnf update
Then, use the following command to install Yarn on Rocky Linux 8:
sudo dnf install yarn
Verify your installation by checking its version:
How To Use Yarn Package Manager
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): orca 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 26.68s.
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 Rocky Linux 8. To do this, switch to the directory and run the following command:
Add a Package with Yarn
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 Rocky Linux 8:
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:
Or you can use:
At this point, you have learned to Install and Use Yarn on Rocky Linux 8.
Hope you enjoy it.
You may be like these articles: