Yarn attempts to fix one of the fundamental issues with npm; consistency across a device. npm (especially v3 and above) tries its best to resolve dependencies as flatly as possible (ie: it attempts to avoid a node_modules/package/node_modules/package/node_modules like structure), but depending on the order you’re adding packages to your install, this can vary wildly between machines. If you’ve ever come across the age old “but it works on my machine” problem, chances are this is why. Yarn takes a different approach to dependency resolution, to ensure consistency in dependency layout/versioning across development environments; writing lock files with ordered keys to ensure any changes are simple to execute. If you develop on Windows, this is especially handy, as it removes the issues with multiple nested directories.
The install process is broken down into three steps:
Resolution: Yarn starts resolving dependencies by making requests to the registry and recursively looking up each dependency. Fetching: Next, Yarn looks in a global cache directory to see if the package needed has already been downloaded. If it hasn't, Yarn fetches the tarball for the package and places it in the global cache so it can work offline and won't need to download dependencies more than once. Dependencies can also be placed in source control as tarballs for full offline installs. Finally, Yarn links everything together by copying all the files needed from the global cache into the local node_modules directory. Ironically, Yarn is installed via npm:
npm install -g yarn
As it’s a drop in replacement, wherever you used
in your workflow, you now use
However, there are some notable exceptions:
npm install becomes just
npm install --save <package> becomes
yarn add <package>
We’ve been using Yarn at Fast Web Media for a couple of weeks, and have found it to be a quick and easy replacement for npm, with the most difficult part being retraining our muscle memory to not type npm! So far, we haven’t come across any package related issues, but if we do, this post will be updated to reflect them.
Have you introduced Yarn into your development team? We’d be interested to know your thoughts - Tweet us @FastWebMedia