Version 2 of the popular JavaScript package manager Yarn has been here for a year now. Yet try to find information on upgrading from 1.X or how it compares to current versions of npm (especially the 7.x dev release), and you’ll get plenty of well-written but outdated blog posts mainly bemoaning the new direction the Yarn team has taken. At least our own Dan Kelch seemed more positive.

On its initial release, taking a wait-and-see approach with Yarn 2 may have been the correct call, but I’m here to tell you that it’s time to reconsider that decision. Here are three reasons you might have waited to make the switch — and why those reasons are out of date in 2021.

1. Backwards Compatibility with node_modules

Yarn 2 uses a new Plug’n’Play (PnP) architecture that is a huge departure from how npm projects have always worked. PnP does away with the node_modules/ folder where packages get installed. Unfortunately, this is incompatible with certain JavaScript-based projects that rely on that directory to find packages, namely React Native, Flow, and VSCode Extension packages.

However, Yarn 2 now offers an option that copies packages to the node_modules/ folder just like Yarn 1, providing backward compatibility for these projects. It literally requires adding a single line to your new .yarnrc.yml configuration file to turn on this feature.

And of course, if you aren’t working on a React Native, Flow, or VSCE package project, this likely isn’t even an issue. The list of popular frontend JS tools that natively support PnP is long and growing.

2. Difficulty Migrating from Yarn 1.x

The PnP incompatibilities that plagued the initial Yarn 2 release — and the seismic shift it created in getting rid of that hefty node_modules/ folder — also meant migration could be a big pain for pre-existing projects. But with node_modules/ support added back in, Yarn 2 now provides a clean migration path. There are a not-insignificant number of steps here, especially if you’re moving to PnP support, but the documentation is clear and easy to follow. It shouldn’t actually be that difficult to undertake.

Also, if you’re starting a brand new project, migration isn’t a concern at all. yarn set version berry is how you should begin any new JavaScript development project.

Berry True
I’m not sorry for that pun.

3. But My Yarn 1.X Setup Isn’t Broken!

Sorry Marge, the mob has spoken. As the Yarn 2 documentation mentions:

Even if you don’t use Plug’n’Play nor plan to use it, your project will still benefit from more stable node_modules layouts, improved performances, improved user experience, active development, and many other boons.

This is no different from other popular libraries or frameworks making huge architectural changes (think React 16.8 with hooks or Python 3). The longer you wait to make the switch, the more painful it will be for your project when you finally do. And in the meantime, you’ll be missing out on valuable improvements to a fundamental part of the workflow of every single project you work on.

A Future Without node_modules

There are plenty of other reasons to be excited about Yarn 2:

  • An extensible plugin architecture allows for customizing your workflow or even making Yarn a package manager for non-JavaScript projects.
  • Workspaces make monorepo-style projects more manageable. (Yeah, npm 7 has these too, but Yarn 2’s implementation is more expressive.)
  • Constraints for dependencies across workspaces. This feature is still experimental but powerful!

Most importantly, though, I believe we’re going to look back on Yarn 2 as the beginning of the end of the node_modules/ folder, and I couldn’t be more excited about it.

The Yarn project started out to fix some of the fundamental issues that plagued early npm projects: non-deterministic package trees, inconsistent installs across different dev environments, and slow installs with extensive package dependencies. Yarn 2’s PnP feature feels like the logical conclusion of that mission, literally eliminating the folder at the root of the problem.

As we like to say in software development: the best time to update your software was the last LTS release; the second-best time is today. What are you waiting for?

My work here is done

The post It’s 2021, and You Should Switch to Yarn 2 appeared first on Atomic Spin.