

Hello Yarn 2, Goodbye node_modules.
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.

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.

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.

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.
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
-
82
Commentary: If you're a "lifelong" iPhone user you might be tempted to skip the iPhone X and get a Pixel 2. And that's OK.
-
57
Note:This PR's discussions should be focused on the high-level design. [A separate PR] ( yarnpkg/yarn#6382 ) has been opened on the code repository to discuss th...
-
60
Both pnpm (as of v2.17) and Yarn (as of v1.12) support fast, concurrent installations in monorepos. However, there is a big difference betwee...
-
7
news view node依赖管理 Node的依赖管理工具 使用一个名为package.json的文件,用户可以通过npm install –save命令把项目里所有...
-
24
Tutorial How To Install and Use the Yarn Package Manager for Node.js Node.js
-
5
升级Yarn 2,摆脱node_modules发布于 今天 03:04 Englishnode项目中最臭名昭著的莫过...
-
11
Goodby...
-
4
17 November 2021 / husky 使用yarn为新node.js工程添加husky Husky官方文档的示例都是使用npm来添加husky,但是,如果使用yarn,要如何添加husky...
-
9
[OUTDATED] Install NVM, NodeJS, Yarn via Homebrew WARNING Dear all Github friends, I moved this gist to the Github repository. Following this repository
-
4
器→工具, 编程语言 Windows Node.js+Yarn的安装...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK