Yarn is a JavaScript package manager. It is a popular tool used by web developers to manage the dependencies of their projects. Package managers are used to manage the dependencies of a project. They can be used to install, update, and uninstall packages. They usually use a repository of packages. This repository can be local or online. Yarn can be used for various languages, including JavaScript, Python, Ruby, and PHP.
Yarn was created by Facebook in 2016 as an open-source project. It was designed to improve on npm, which was the most popular JavaScript dependency manager at the time.
One of the main goals of Yarn was to improve performance and reliability. It does this by caching packages and using a deterministic installation algorithm.
Yarn is also popular because it offers a more secure experience than npm. This is because it uses checksums to verify the integrity of each package before installation. Additionally, it encrypts all communications between Yarn clients and servers.
In general, Yarn and NPM differ in some fundamental ways:
- The first is performance. yarn is faster than npm because it uses a caching mechanism. Additionally, it uses a deterministic installation algorithm, meaning that installations will be the same regardless of the machine or environment you use.
- The second difference is security. yarn is more secure than npm because it encrypts all communications between clients and servers, and verifies the integrity of each package before installation.
- The third difference is reliability. yarn was designed to improve reliability by caching packages and using a deterministic installation algorithm. This means that installations will be the same regardless of the machine or environment you are using.
This article will show you how to install Yarn on AlmaLinux 8 and 9. A brief introduction to Yarn commands and options will also be provided.
Prerequisites
It is essential that you have the following to follow this article:
- A server with AlmaLinux installed.
- Having a working knowledge of Linux would be a plus and be familiar with the command line interface.
- Superuser or root privileges on your server.
Updating the System
Keeping your system up to date is always a good idea before installing new software. To do this, you can use the following command.
sudo dnf update
Once the system is updated, run the command below to install the Extra Packages for Enterprise Linux (EPEL) repository.
sudo dnf install epel-release
Installing Node.js
V8 JavaScript is the engine behind Node.js. It enables developers to write server-side applications in JavaScript.
You have to install Node.js first before you can install Yarn. Run the node -v and npm -v commands below to verify that Node.js and npm are installed on your system.
node -v npm -v
If you see a version number like v8.11.3, then Node.js and npm are installed on your system.If you don’t, you’ll get a command not found error. In this case, run the following command to install Node.js.
nodejs:14 is the version of Node.js being installed on your system. If you would like to use another version, provide the correct number.
A module in the command is a community-maintained package repository that contains many different Node.js modules and dependencies.
sudo dnf module install nodejs:14
To verify the installation, you can run the node -v and npm -v commands again.
Sample output:
Installing Yarn on AlmaLinux
Now that you have Node.js installed, you can install Yarn. The Yarn developer recommends that you should install Yarn globally using npm. Yarn is installed globally via the -g option. This means that you can use it from any directory.
sudo npm install -g yarn
After the installation, you can verify Yarn’s version by running the following command.
yarn -v
Sample output:
The yarn -h command provides more information about using Yarn commands.
yarn -h
Sample output:
Creating a New Yarn Project
The yarn command has been installed globally, and you can use it to create a new project to test your Yarn installation. The command below creates a directory with myyarnapp, where you will store your project files.
mkdir myyarnapp
Now, move into the newly created directory and initialize (init) a new yarn project.
cd myyarnapp && yarn init
This init command will ask you a few questions about your project, such as the project’s name, description, and author. If you wish to accept the default values for each question, you can press Enter.
A package.json configuration file and a yarn.lock file will be created in the directory.
The package.json file contains information about your project, such as the project’s name, version, description, dependencies, and more. The yarn.lock file is used to lock down the exact versions of your project’s dependencies. This ensures that you always get the same results when you install these dependencies, regardless of the machine or environment you are using.
The next step is to install all of the project’s dependencies. You can do this by running the yarn install sub command.
yarn install
This will install all of the dependencies listed in the package.json file.
To add a new dependency to your project, run the following command. The name of the package that you would like to add is package-name.
yarn add package-name
For example, if you want to add the React library to your project, you would run the following command. The React library provides an interface for creating UIs and interacting with them.
yarn add react
The React library will now be installed as a dependency of your project. The package.json file will be automatically updated with the new dependency.
By default, the yarn add command installs the latest version of the package if you do not provide a version number.
To install a specific version of the package, you can use the $version option
yarn add [email protected]
For example, you would run the following command to install the React library version 16.2.0.
yarn add [email protected]
Sample output:
To remove a dependency from your project, run the following command. Where package-name is the name of the package that you want to remove.
yarn remove package-name
For example, you would run the following command to remove the React library from your project.
yarn remove react
The React library will now be removed as a dependency of your project.
The yarn remove command removes the specified package from your local node_modules directory but doesn’t remove any version information stored in the yarn.lock file.
This means that if you reinstall the package by running yarn install, Yarn will use this same version number again even though a newer version of the package may be available.
You can upgrade Yarn to a different version with the help of the curl command.
curl --compressed -o- -L https://yarnpkg.com/install.sh | bash
The command above will download the Yarn tarball, extract it, and run the included install.sh script. This script will upgrade Yarn to the latest version.
Conclusion
In this article, you learned how to install Yarn, create a new project, add dependencies to your project, remove dependencies from your project, and upgrade Yarn. You also learned about the package.json file and the yarn.lock file.
To learn more about Yarn, see its official documentation.