Yarn Package Manager in 2019: Should We Keep on Comparing Yarn with Npm?

Never miss a story from Soshace
Looks good!
Please enter correct name
Please enter correct email
Looks good!
Please confirm to subscribe!

Yarn Package Manager in 2019: Should We Keep Comparing It with npm?

For many web developers, their package manager is an indispensable tool: it simplifies their development workflow and unifies their work experience. Among these managers, npm has been reigning supreme for quite some time: it is currently the golden standard for package management, signified by the popularity of the “npm install” search queries.

However, npm does have its drawbacks. To address npm’s issues, other package managers were created. One of them is Yarn, which Facebook, Exponent, Google, and Tilde created to improve upon the package management workflow. In this article, we’ll explore Yarn thoroughly and answer these questions: what are Yarn’s key features? What advantages and drawbacks does Yarn have? How is Yarn workflow organized? Let’s find out!

The bigger picture: why was Yarn created?

Yarn Package Manager in 2019: Should We Keep Comparing It with npm?

Yarn’s history is closely tied to the general JavaScript developer workflow — and the problems it incurred. As developers share millions of code elements, managing the dependencies between them became a crucial task, so package managers stepped in. For quite some time, the developers at Facebook have been content with npm, the most popular package manager. However, time passed and the size of developer teams grew manyfold — and so did Facebook’s codebase. Using npm in this environment only led to more problems: security issues, inconsistencies, and suboptimal performance.

At first, the devs tried to scale the npm client: only checking package.json and encouraging developers to manually run npm install. Even though it was a good practice, it failed to adequately perform in Facebook’s continuous integration (automating the running of code, tests, and/or builds on a separate machine) environments — these environments were meant to be sandboxed for security reasons.

At some point in time, the teams had to conclude that npm wasn’t working for them, so they chose to developer their own solution instead. Teaming up with software engineers from Exponent, Google, and Tilde, they quickly discovered that the type of problems they were experiencing was quite common — and so Yarn was born.

Key features & differences

Yarn Package Manager in 2019: Should We Keep Comparing It with npm?

When talking about one package management solution, its competing counterparts are inevitably mentioned: so when we want to discuss Yarn’s key features and differences, we often have to compare them against those of npm. Like in many heated discussions that follow the “A vs. B: What’s Better?” pattern (our recent article about React vs. Angular is a good example, have you read it?), developers like to compare Yarn and npm directly, all the while trying to determine the ultimate winner. However, our experience favors a different approach: balancing web developer tools.

Although Yarn and npm perform the same function, there are certain areas where one gets the upper hand over the other. So what’s great about Yarn?

Speed. Caching every downloaded package, it avoids the need to re-download them later. Additionally, Yarn maximizes resource utilization via concurrent processes, allowing for faster installs.
Reliability. Thanks to the lockfile format and a deterministic manner of installing operations, Yarn ensures baseline installation across all systems.
Security. The integrity of every installed package is verified via checksums — this is done before any package code is executed.

  • Support for both npm and bower workflows, allowing to mix registries
  • Users can restrict licenses of installed modules…
  • … and output license information
  • Easy-to-read CLI output
  • Offline mode allows for re-installation of packages without an internet connection.
  • Unified installation structure independent from installation order.
  • Improved network performance via queuing requests in an efficient manner (and avoiding request waterfalls altogether)
  • Improved network resilience via preventing individual failed requests from stopping the entire installation; instead, failed requests are automatically retired.
  • Elimination of duplicates via resolving mismatched versions of dependencies to a single version.
  • More emojis. Probably should’ve put it at the top of the list…

Yarn workflow

Yarn Package Manager in 2019: Should We Keep Comparing It with npm?

How to install? Although a classic command like npm install -g yarn can be used for installation, the Yarn team advises against it: it provides separate installation methods for various operating systems. Then, we can finally use the yarn command in the shell: if not given any arguments, this command will read the package.json file, fetch packages from the npm registry, and fill the node_modules folder. In essence, this command is equivalent to npm install.
How to manage packages? Similar to npm, Yarn logs the dependencies in the package.json file (located in the project’s root folder), while the dependencies files themselves are stored in the node_modules folder.
How to initialize a new project? Running yarn init will call an interactive prompt that will guide us through the project’s initial set-up:

How to inspect licenses? With yarn licenses ls, you can see the licenses of all project dependencies. It can also generate a disclaimer with yarn licenses generate-disclaimer.
How to inspect dependencies? With yarn why, you can learn why a specific package was installed.

Still, Yarn can sometimes feel limiting or even subpar in certain areas. While it strived to improve upon the insufficiencies of npm, it happened to create some problems of its own: disk space usage, for instance.

Many developers find it jarring that their rather small projects (no larger than a few hundred lines of JavaScript code) can easily turn into 100 MB monstrosities when used with modern tooling (Babel, Webpack, or React). A fitting example is the node_modules directory, clogging up disk space with various test files, example directories, and build scripts. Some of these “miscellaneous” files can be cleaned with the clean command: yarn clean. This creates a .yarnclean file which defines the file types to be deleted. These are the default settings — Yarn will delete these files and categories:

As long as the .yarnclean file stays in the project root directory, Yarn will run a cleaning task after every install — and inform how much space it saved.

Conclusion

Although Yarn doesn’t boast the same advantages over npm (as it did back in 2016-2017, before npm version 5), it’s still a very solid choice for web developers. Now that Yarn and npm are becoming ever so similar, the developers can finally appreciate both of these tools and use either of them accordingly.

Never miss a story from Soshace
Looks good!
Please enter correct name
Please enter correct email
Looks good!
Please confirm to subscribe!

Related articles

Web Usability Essentials

Web usability and usability testing are those few buzzwords you can't afford to ignore. In this piece, we'll review the basic principles of web>>>

Fundamentals
13.08.2019

Overview of Basic Data Structures: How to Organize Data the Efficient Way

We’ll focus on the most fundamental and important ones — arrays, linked lists, stacks, and binary trees. In this article, we’ll visualize how>>>

Fundamentals
7.08.2019

WordPress for Non-Programmers: Introduction to the Web Development World

Nowadays, WordPress is the ultimate answer to the “How can I easily build a website?” search query. This article will guide you through the>>>

Fundamentals