Computer scienceBackendNode.jsModules and DependenciesPackage managers

yarn

6 minutes read

In this topic, we will learn about Yarn — a package manager for JavaScript. We will start by reviewing some basic concepts about JavaScript and package managers. You will understand "What is JavaScript?", "Its role in web development", and "What are package managers?". Then, we will introduce Yarn and discuss how it works along with its features. We will also compare Yarn to npm and explain why Yarn is the better alternative. In addition, we will look at the steps of using Yarn, including how to install it, initialize a project, install packages, and manage dependencies. Finally, we will talk about some drawbacks and limitations of Yarn to give a comprehensive overview of the tool.

Brief reminder

Reviewing some basic concepts about JavaScript and package managers will help you understand Yarn.

  • JavaScript is an interpreted, object-oriented, and dynamic language used for client-side and server-side development. It interacts with web page elements and is executed by a JavaScript engine. An engine such as v8 is used in Chrome and is the core of Node.js — a runtime environment for building server-side applications.
  • A package manager is an automated system for installing, upgrading, configuring, and using other programs.
  • Npm is the default package manager for Node.js — a Javascript runtime environment. Node.js consists of the npm command-line client and an open-source and private package database known as the npm registry.

What is yarn?

Yarn is a package manager created to replace the npm client workflow while still using the npm registry. Facebook developed it after they attempted to scale npm. So, it has the same features as npm while also running faster, more securely, and more reliably.

Yarn logo

The primary function of Yarn is to install packages from a global registry into a local environment. Packages may depend on other packages and can be organized into a tree of dependencies. A package includes shared code and a package.json file that describes the package.

The Yarn package installation involves three steps: resolution, fetching, and linking. The resolution step determines the dependencies of the requested package. The fetching step checks the global cache directory for the needed dependency packages, downloads missing packages, and stores them in the cache for future use. In the linking step, Yarn links everything and copies the needed files to the node_modules directory of the project.

Why choose yarn? yarn versus npm

Yarn offers several advantages when compared with npm. While npm has been the more commonly used package manager for JavaScript for many years, Yarn has quickly gained popularity since its release in 2016 and has a more active and growing community. Yarn boasts faster performance and improved security measures, such as verifying packages using checksum and supporting the Zero-Install feature. The Zero-Install feature allows you to install dependencies while offline with minimal latency. Additionally, Yarn supports parallel installation of dependencies. It has a version lock file called yarn.lock and the Plug'n'Play feature, which generates a .pnp.cjs file with a map of dependencies for the project.

However, Yarn does have some drawbacks compared to npm. It may have issues installing native modules of Node.js and does not support versions lower than 5. For developers who are used to the npm workflow, switching to Yarn may take time and effort to adjust to the new commands and workflow. Overall, while both Yarn and npm have their strengths and weaknesses, the choice between them ultimately comes down to personal preference and the specific needs of your project.

How to use yarn?

Using Yarn as a package manager is straightforward and involves several basic steps. The Yarn package manager is built on top of Node.js. Therefore, you need to have Node.js installed on your system to use Yarn.

Installing Yarn: Once Node.js is installed, you can install Yarn using the appropriate command:

npm install -g yarn

This command installs the Yarn package globally on your system and makes the yarn command available on your command line. This command should be run in your terminal or command prompt. Installing Yarn globally means that the Yarn package manager is installed on your system and can be accessed from anywhere rather than just within a specific project. This allows you to use Yarn's command-line interface to manage packages and dependencies for any project on your system.

Initializing a project: To initialize a project and create a package.json file, navigate to the project directory and run the following command:

yarn init

Installing packages: To install a package, you can use the add command. For example, installing the lodash package, you would run the following command:

yarn add lodash

Updating packages: To update a package, you can use the upgrade command. For example, to update the lodash package to the latest version, you would run the following command:

yarn upgrade lodash

Removing packages: To remove a package, you can use the remove command. For example, to remove the lodash package, you would run the following command:

yarn remove lodash

Checking installed version: Displays the version of Yarn:

yarn --version

or

yarn -v

Running scripts: You can also use Yarn to run scripts defined in the package.json file. For example, to run the build script, you would run the following command:

yarn run build

Following these steps, you can effectively use Yarn as a package manager in your JavaScript projects.

Let's say we need a new package from the online registry. This is how the whole thing will look in the terminal:

1. Installing Yarn — npm install -g yarn

Installing Yarn

2. Moving to the project folder — cd folder-path

Moving to the project folder

3. Installing needed files from package.json to node_modules and generating yarn.lock — yarn

Installing Yarn  -2

4. Installing Jest — yarn add jest

Installing Jest

With this, you can successfully install Yarn and add Jest to your computer. You can follow the link and read the official documentation for the full list of commands.

Conclusion

Yarn is a popular package manager that simplifies the development process by allowing developers to share code quickly and securely. It has several advantages over npm, including faster performance, improved security, and better dependency management. However, you may face issues while installing native modules of Node.js. Yarn also does not support older versions. In summary, the choice between Yarn and npm depends on the needs and preferences of the developer. Both have pros and cons, but it might be worth trying out to see which works best for a particular project.

9 learners liked this piece of theory. 2 didn't like it. What about you?
Report a typo