The switch from npm to Yarn is a straightforward process. It was built by engineers at Facebook, Google and a few other institutions. It was created to fix a few problems npm faced around speed, consistency, and security. The Yarn CLI is nearly identical to npm with a few new commands.
Migrating to Yarn
The migration process is done through running the yarn command then committing the following yarn.lock file. A package.json file contains all the new code which will begin the switch. The installation process can be found directly from the website. Here it lists the different ways to download it on various operating systems. You’ll need to have Node.js already downloaded for the installer to work on most systems. This shouldn’t be a problem as you’re already switching from npm.
In most cases, yarn will work right the first time. In the event that it doesn’t work on a larger project, other developers can keep using npm. Developers on a project can convert at different times from npm to yarn. Some developers may find yarn isn’t right for them at the moment. In the event that this is true, all developers have to do is delete the yarn.lock file. Although you’ll probably enjoy the new format and speedier compile times with yarn.
There is one problem of note in npm. It stems from the package.json, the file that both npm and Yarn use to track a project’s dependencies. Versions aren’t always exact in this file. This means that two different computers that have the same package.json file will have differing versions of the package installed on their machines. This can create discrepancies in the code and may cause bugs.
In order to avoid these versioning problems, a precise version is downloaded into a lock file. The previously mentioned yarn.lock file is created for this very reason. Now it is guaranteed that a package will be downloaded the exact same way with the same contents in the package.json.
Yarn creates and updates the yarn.lock by default. That isn’t the case with npm as it only can create a lock file through the shrinkwrap command. This command isn’t set by default and does not automatically update the package.json.
When npm or Yam must install a new package, it does so through a set of tasks. With npm, tasks are completed in a series of packages. This means that one must be installed fully before it goes to the subsequent package. Yarn on the other hand installs tasks with a parallel download. This greatly increases performance. Developers can try it out and see for themselves.
For example, downloading Express took 9 seconds in npm compared 1.33 seconds in Yarn. The gulp package also came in with similar results with npm at 12 seconds and yarn at 8.23 seconds. These numbers may differ compared to your setup and whether or not your cache has been cleared. Yarn is consistently faster than npm.
Another major difference is the advent of some new CLI commands not found in npm. Here are some of the most popular ones in use.
yard add: adds a new package to the current in-use package.
yarn init: initializes a new development in the package.
yarn install: installs dependencies defined in the current package.json file.
yarn publish: publishes the package to the package manager.
yarn remove: removes unused packages from the current package.
Overall, moving from npm to Yarn is simple and worth the effort. Learn the new commands and you can reap the benefits of the new speed and security in Yarn.