In application development, we often prefer to use existing software packages to make our tasks easier and faster. This is where package managers come into play. They allow us to create, update, reuse, and share software packages. Among Node.js and Javascript developers, the two most popular ones are npm and Yarn. The former is installed by default when Node.js is installed. The latter can be downloaded as a package via npm.

We know that npm is still the most widely used package manager, but at the time it had two notorious deficiencies: its speed, and its lack of predictability in the order of installation of dependencies. Although the second has already been solved, the first persists when you have large projects. For these reasons Facebook launched Yarn in 2016.

This lack of predictability was the main reason why, at the time, many developers migrated to Yarn in the beginning, because, when using npm, in each installation the order of each package and its dependencies could be different. That could cause that two developers of the same project get node_modules folders with different structures despite being created based on the same package.json file. Thus, the same code would work on one developer’s PC but not on another. Yarn solved this problem by creating the yarn.lock file on the first install, which determined the proper order for the following installations. Given this fact, npm has been updated by incorporating its package-lock.json, which fulfills the same function.

But, as I said, the performance issue when installing packages is still around. Yarn has been able to fix it by being optimized to fetch and install packages in parallel. However, npm still opts for a serial one, which, despite the performance improvements included in its versions 5 and 6, still does not rival that of Yarn. Likewise, Yarn includes another important improvement in this regard: offline reinstallation, which makes use of cached packages on the user’s disk. While many may not be OK with this use of disk space, it gives us the advantage of avoiding the overhead of having to send HTTP requests to get packages that have been installed before, which is exactly what happens if we use npm.

Regarding the CLI, Yarn’s console output is much cleaner than that of npm. Even when using emojis except when used in Windows. And regarding the commands, we can find the following:

  • yarn why: This command allows us to identify why a package has been installed. It details the packages that depend on it, or if it is explicitly defined as a dependency in the package.json.
  • yarn licenses: It allows us, by means of its list modifier, to list alphabetically all the packages installed by Yarn together with the license and URL of the source code corresponding to each one.
  • yarn upgrade-interactive: This command will display obsolete packages. It allows the user to select which ones to upgrade respecting the version ranges in package.json when determining the version to upgrade to or directly upgrading to the latest version with the –latest flag.

CONCLUSIONS

In conclusion, although Yarn’s use of disk space can be a disadvantage, it is more than compensated for by the advantages it offers over npm. Although many of the shortcomings that the latter presented at the time have been already tackled, it continues without being able to reach Yarn in terms of what it offers, which has proven to be more efficient today. Since our projects tend to grow too large, the optimizations that Yarn offers on npm make it the perfect package manager to use. Also, despite the fact that Yarn is relatively new, its adoption by developers is growing exponentially. That means that help can easily be found when in problems. In addition, since January 2020 Yarn 2 (also known as Bery) has been available, which includes various improvements such as the Plug’n’Play installation mode, which allows optimizing the installation speed when executing the yarn install command, regardless of the typical node_modules folder; a modular structure based on plugins, and more. So, in short, Yarn is the best option overall, and it seems to be the future of package managers for JavaScript.