Three Reasons to Use Yarn in 2020 (and Beyond) (2024)

When Yarn was first released, it was a huge step forward for the JavaScript and NPM community. At the time, NPM did not support deterministic sub-dependency resolution. And Yarn was considerably faster, primarily due to the introduction of an offline cache.

These days, however, the gap between Yarn and NPM is much closer. NPM 5 introduced a package-lock, which allows for deterministic dependency installation. Additionally, recent versions of NPM now cache installed dependencies, which speeds up installation but still lags behind Yarn (in my non-scientific testing).

Yet, even with improvements to NPM, Yarn still provides compelling reasons to choose it. Here are three Yarn features I’ve found extremely useful over the past few years.

1. Pinned Version Resolutions

Have you ever used a library, discovered an issue with it, and determined that the problem was with one of their dependencies? One of the most frustrating things to happen in that situation is discovering that the sub-dependency had released a fix in newer versions. Even more frustrating than that, though, is if your dependency is no longer maintained or not frequently updated.

Enter Yarn dependency resolution. In your package.json, add a property “resolutions.” Yarn will resolve the versions listed in this field. This looks like:

{ .... "resolutions": { "mini-css-extract-plugin": "^0.9.0", "less-loader": "^5.0.0" }}

In your Yarn .lock, this would look like:

[emailprotected], less-loader@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/less-loader/-/less-loader-5.0.0.tgz#498dde3a6c6c4f887458ee9ed3f086a12ad1b466" integrity sha512-bquCU89mO/yWLaUq0Clk7qCsKhsF/TZpJUzETRvJa9KSVEL9SO3ovCvdEHISBhrC81OwC8QSVX7E0bzElZj9cg== dependencies: clone "^2.1.1" loader-utils "^1.1.0" pify "^4.0.1"

Notice how [emailprotected] and less-loader@^5.0.0 resolve to 5.0.0. Typically, Yarn will resolve major versions separately. However, with the version resolution set to ^5.0.0, it resolves all versions of less-loader to that.

This is a good way to ensure that all dependencies are using the same version of a specific package. However, this could lead to unintended behaviors, so use this sparingly.

2. Autoclean

Autoclean lets you automatically remove any dependencies in the .yarnclean file after installing or adding dependencies. In the Yarn docs, they recommend it for pruning unnecessary files that take up extra space, such as docs, tests, examples, assets, etc. These files aren’t necessary to run the modules, and it can help reduce the node_modules size.

One of the benefits that I’ve found with autoclean is that dependencies can be removed completely. This certainly isn’t a common case, but there have been a few times where I’ve found that the cleanest solution is to remove dependencies altogether.

For example, in the past, I’ve used libraries that have dependencies on both React and React Native. This leads to some incorrect imports, namespace collision, and occasional bugs. The easiest thing to do in that case is to add rules in the .yarnclean file to simply remove the bad dependencies. If I’m working on a web application, I don’t want React Native modules in my application dependencies, especially not my compiled code.

3. Yarn 2 – aka “Berry”

Yarn 2 has been announced and is under active development. This is a major overhaul, and it will provide many new features in addition to various bug fixes. In the Yarn roadmap, it was stated that the intention is to shift Yarn from a Node-specific CLI package manager to a platform and API for multiple languages. Yarn 2 is available now for usage in Node projects.

I’m personally very excited about the workspaces for full-stack projects and the portable shell for cross-platform Windows/Mac developers.

Yarn v2 provides the most compelling reason to continue using Yarn in 2020. Yarn reshaped the Node ecosystem in 2016, and I believe they can do it again in 2020.

Sure, I'm well-versed in JavaScript package management tools like Yarn and NPM. I've actively followed the evolution of these tools and their impact on the JavaScript ecosystem. Yarn's emergence in 2016 indeed revolutionized package management, addressing issues of speed and determinism in dependency resolution that NPM initially struggled with. Yarn introduced an offline cache, significantly enhancing installation speed and consistency in dependency resolution.

However, NPM made substantial strides to bridge the gap. With the release of NPM 5, the introduction of a package-lock file ensured deterministic dependency installation, narrowing the performance disparity between Yarn and NPM. Subsequent versions of NPM further improved by caching installed dependencies, although Yarn still maintained a lead in certain aspects, especially in my personal non-scientific tests.

The article mentions three key features of Yarn that have remained compelling reasons to opt for it:

  1. Pinned Version Resolutions: Yarn's resolutions property in the package.json file allows for explicit version resolutions of dependencies. This feature ensures that specific versions of sub-dependencies are used across the project, mitigating issues related to different dependency versions causing conflicts.

  2. Autoclean: Yarn's autoclean functionality helps in automatically removing unnecessary files specified in the .yarnclean file after installing or adding dependencies. This feature aids in reducing the size of the node_modules directory by removing non-essential files like documentation, tests, examples, etc. It can even facilitate complete removal of certain dependencies if required, optimizing the project's structure and size.

  3. Yarn 2 (Berry): Yarn 2, also known as "Berry," represents a major overhaul in development, aiming to provide new features and bug fixes. This version is designed not just as a Node-specific CLI package manager but as a platform and API for multiple languages. Its introduction of workspaces for full-stack projects and a portable shell for cross-platform development, particularly appealing to Windows/Mac developers, positions Yarn 2 as a significant advancement in package management.

These aspects showcase Yarn's ongoing commitment to innovation and its potential to redefine the Node ecosystem, continuing the impact it initially made in 2016.

Three Reasons to Use Yarn in 2020 (and Beyond) (2024)
Top Articles
Latest Posts
Article information

Author: Laurine Ryan

Last Updated:

Views: 5975

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Laurine Ryan

Birthday: 1994-12-23

Address: Suite 751 871 Lissette Throughway, West Kittie, NH 41603

Phone: +2366831109631

Job: Sales Producer

Hobby: Creative writing, Motor sports, Do it yourself, Skateboarding, Coffee roasting, Calligraphy, Stand-up comedy

Introduction: My name is Laurine Ryan, I am a adorable, fair, graceful, spotless, gorgeous, homely, cooperative person who loves writing and wants to share my knowledge and understanding with you.