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.