Yarn — The Future is Here
The Facebook Developed Package Manager
A few months ago I wrote a post comparing package managers, npm and bower to be specific. Since then, I keep seeing articles and posts about a newcomer to the game: yarn.
So, what is yarn?
Yarn is a package manager more akin to npm than bower (remember, bower just manages front-end assets). It was developed by the masterminds at Facebook because, as they say, they “ran into problems with consistency, security, and performance” with npm as the size of their team and codebase grew. Their solution: let’s build our own!
The beauty of yarn is in it’s simplicity and speed. To get started, use npm (ironically) to install (the
-g is to install it globally) or homebrew:
npm install -g yarn
brew install yarn
It’s compatible with all of the packages you would normally install via npm or bower. To add these packages to your project, you create a
When you want to add a dependency, searching on yarn may be a good place to start. You always want to be sure you get the name correct. So when you want to add angular, enter:
yarn add angular
This will automatically save the dependency to your
package.json, no need to add
--save to the command. Again, the simplicity is so nice. Once you have all of your dependencies, a simple
yarn install will install them for your project. Here’s where the biggest difference between npm and yarn is most evident.
Yarn is fast…like, really fast!
Depending on the size of your project, and the number of dependencies you have, you’ll notice a significant increase in install time. I personally haven’t tested it out on a project yet, but some articles I’ve read indicated that yarn could potentially be 2–4 times faster than npm. That’s crazy! And…how is that possible?! It turns out that whenever you install a dependency (package) with yarn, it places it in a cache on your system. That means, theoretically, you could be working offline and still be able to install all the dependencies you need for your project assuming you’ve installed them before for another project. That’s just plain smart!
Founded by actor and writer Amy Poehler and producer Meredith Walker, the Smart Girls organization is dedicated to…giphy.com
These reasons are enough to convince me to try yarn with my next project, most likely in Rails. Oh, you didn’t hear? Rails 5.1 will come prepackaged with yarn support via the
webpacker gem, bypassing the asset pipeline. Definitely something to be on the lookout for.
In the meantime, here’s a handy comparison/cheatsheet between npm and yarn I came across. Enjoy!