15

Hello Yarn 2, Goodbye node_modules.

 3 years ago
source link: https://freddixx.medium.com/hello-yarn-2-goodbye-node-modules-21d97afc1a75
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Hello Yarn 2, Goodbye node_modules.

What’s new (and the least painful farewell in the history of JS)

Over three years go, in 2017, Yarn 1.0 was officially announced in the Facebook Engineering Blog. Already by that time, only 11 months after its first release, more than 175,000 repositories had started using the new package manager.

Even though Yarn’s success story has continued steadily ever since, the tool has also fallen victim to many of the classic package handling weaknesses such as slowness, increased complexity and data footprint.

Yarn 2 comes with a few fundamental changes which not only address these points but also improve your overall workflow.

1*NhZ8Ts1Xe0SlreW2Ljzo6g.png?q=20
hello-yarn-2-goodbye-node-modules-21d97afc1a75
Not seeing any memes like this anymore will be the saddest part about Yarn 2.

What’s new in Yarn 2?

The new version had already been released last January (2020 that is), however it took most bigger projects and libraries a good while to adjust to the new Plug’n’Play way.

And while some large names are still struggling to reach compatibility (as of the time of writing: Angular, React Native and Flow), now is a good opportunity to try it out in your codebase.

Plug’n’Play support isn’t the only big change coming with Yarn 2 (the concept in itself isn’t even all that new and dates back to September 2018), the update actually ships a lot of great features which make life a lot more simple.

Yarn’s maintainers poured full year of their time into these improvements and you can definitely feel that Yarn 2 is a well-thought-out product.

Actual debuggable CLI output

It starts with the CLI output which is now much more structured. While yarn console texts were previously a challenging visual wild west at best, formatting and colours now contribute to a much improved readability.

1*9pD2aG7jyTayLyfTeRKoLg.png?q=20
hello-yarn-2-goodbye-node-modules-21d97afc1a75
Yarn’s CLI output is not a disorganised wall of text anymore.

On top of that, every line features its own error code and hence becomes a lot easier to debug. So time for you to bookmark this list.

It’s not just node_modules

Of course, reducing Yarn 2 to the sheer deprecation of a single dreaded folder would not do this version justice. But hey — it’s gone. And that’s a big milestone.

The new version significantly reduces the amount of file I/O which leads to some impressive time savings during installation.

Yarn now holds a map inside a single pnp.js file which keeps track of package references and will make sure node gets acquainted with the right location once you run your code.

Still, those downloaded packages need to go somewhere on your disk. This is what the new .yarn folder is for.

When node_modules is finally gone.

That location is where those dependencies of yours are cached from now on, but — and I recommend measuring it on your own projects — with a much lower footprint.

Yarn dlx

Running a remote, non-installed package directly was, up until this point, something you had to utilise npm’s npx for. Now Yarn finally ships its own command do to that.

With Yarn 2, you can use yarn dlx to execute a package in a temporary environment without having to install it first.

Patch Protocol

Patching a dependency locally has now become much easier. You can instruct Yarn to extract a package into a temporary folder and then generate a file containing the diff once you are done:

yarn patch node-fetch
yarn patch-commit /private/var/patchfolder > mypatch.patch

Since the patch directory is never used directly there is little space to mess things up. You will only refer to your patch file going forward:

{
"dependencies": {
"node-fetch": "patch:[email protected]#./mypatch.patch"
}
}

This even works for indirect (transitive) dependencies making it a great feature to patch security issues until official fixes are released!

More documentation can be found here and here.

Release Workflow

If you are working in a multi-workspace setup (like a monorepo for instance), releasing and subsequently upgrading all consuming packages in your tree can become a tedious job to do.

Ideally you release a new version of a workspace and all dependents just get auto-updated with the new release.

This is exactly what the Version plugin does for you. It unlocks the new release workflow takes away the pain of having to figure out what to upgrade and where.

Don’t wait. Start migrating now.

Depending on the complexity of your packages migrating to the new version of Yarn (aka modern or berry) might take you a while, but it’s worth the hassle.

If you need some more good reasons: Make sure you check out this list.

If you are already convinced: Take a peek at the migration guide.

This update is just good for everyone involved: You. Your team. And your hard drive.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK