Ocavue's Blog
Yarn 2 first impression 2020-12-25

Yarn is a very popular package management tool for the JavaScript community. On 2020-01-24, Yarn 2 was officially released (opens new window). After observing it for a while, I have upgraded several of my projects to Yarn 2 since last month. In this post, I will briefly describe my experience with Yarn 2 over the past month.

Note: This is not a Yarn 2 tutorial, and I will not go into detail about every feature of Yarn 2. If you want to learn more about Yarn 2, check out the official documentation (opens new window) for Yarn 2.

# Plug'n'Play

Without a doubt, the best feature of Yarn 2 is its Plug'n'Play system (hereafter referred to as PnP). While Yarn 1 can also use PnP, Yarn 2 has it turned on by default.

node_modules is one of the most widely criticized aspects of Node.js. Yarn PnP is one of the community's attempts to replace node_modules. After the first installation of dependencies, Yarn 2 creates a .pnp.js file in addition to yarn.lock. With this file, Yarn can proactively tell Node.js where a specific dependency should be found, rather than letting Node.js try to find the dependency in the node_modules directory on its own. In this way, Yarn can store all packages in a zip format, or even have different projects share the same dependency file, thus reducing the size and loading time of dependencies. From my experience, Yarn PnP is really fast and amazing.

However, almost all of the current Node.js community tools are based on node_modules. This causes Yarn PnP to be incompatible with many current tools, such as code editors. If your editor cannot resolve dependencies, then you need to take extra steps, which include adding extra files to your VCS, as well as manually configuring your editor, or even installing extra editor plugins. Not to mention, as of today, Yarn only supports VSCode and Vim.

Because of these shortcomings, I went back to the node_modules model (by adding nodeLinker: node-modules to .yarnrc.yml). Although given the speed of Yarn PnP, PnP is great, but I don't think the world is ready for it yet.

# yarn.lock and yarn deque

One of the biggest problems with Yarn 1 is the problem of duplicate and outdated dependency versions in yarn.lock. This problem used to cause some very puzzling phenomena. In the past, I have to use yarn-deduplicate (opens new window) from Atlassian to fix this problem. Yarn 2 has native support for the yarn deque command, which has solved this problem at the root.

# Conclusion

Overall, Yarn 2 is quite satisfying. I will continue to use Yarn 2 in a few of my large projects and keep watching.