DEV Community

Cover image for Why I Use Yarn, Not npm
Josh Pollock
Josh Pollock

Posted on • Edited on

Why I Use Yarn, Not npm

Update: This post is based on using Yarn 1. A major update Yarn 2 has been released. I use Yarn 1.x.

In the comments of my post about things I do everytime I start a Gatsby site, there was a really good comment asking why I switch the scripts from npm to Yarn. Great question, different topic, so I wrote this post to share a few reasons I switched.

Historically, the npm CLI is the most common tool for managing dependencies and scripting in a node project. But, there are other CLIs that expect the same package.json schema, for example Yarn or pnpm.

Three Reasons I Like Yarn More Than npm

Before I get to the advantages to my workflow, I have to say part of this is beacuse of the company npm. Last year they braught in a new CEO and laid off a several long-term employees to stop union organizing, which is gross. I'm not going to recount the whole story, but it doesn't make me feal great about a key component of the JavaScript ecosystem.

But, I think it's important to support alternative package managers like Yarn. Yarn and npm default to npm's registry, but also support alternative package registries. I think these problems will be solved with more decentralized registries and multiple CLIs like Yarn.

Anyway, that aside, I think Yarn is very good software for a few reasons.

Run Scripts Slightly Easier

Honestly, this was the first reason I started using Yarn. If package.json has a registered script called "build" you can run it with npm like this:

npm run build
Enter fullscreen mode Exit fullscreen mode

Or with Yarn:

yarn build
Enter fullscreen mode Exit fullscreen mode

Small difference, but I think the time savings adds up. Simpler is better.

Upgrade Interactive

Got a project that is pinned to a bunch of out of date dependencies? While have I got the CLI command for you. Yarn's upgrade-interactive is the hottest command in automated upgrades.

To upgrade one, some or all of your dependencies to the latest version.To bring up a set of prompts to determine which dependencies to upgrade, just type:

yarn upgrade-interactive --latest
Enter fullscreen mode Exit fullscreen mode

Make your selections and watch it install.

Workspaces

Monorepos allow you to develop multiple packages in one git repo. If the packages are related or fairly interdependent, this can speed up development. Depending on the needs, I like to do this with Yarn workspaces or Yarn workspaces and Lerna.

A yarn workspace is a folder in your package with its own package.json. you can use the workspace command to run a script

Yarn Is Good

I've experienced less frustration with dependency resolution, and other errors since switching to Yarn. Also, it makes working with monorepos easier, for me.

Have you switched? If so, did you have a different reason? Disagree? Comments section is below 👇 or @ me on Twitter.

Top comments (16)

Collapse
 
x1k profile image
Asaju Enitan

living in a country with expensive internet data, I feel like Yarn was made for me🙂.
Say for example, I forgot that I have a package installed, trying to reinstall with yarn will exit with it telling me it's been installed, also, if I had a network error while installing packages, it doesn't reinstall all the packages, it installs the specific one needed, I could go on and on about this. But the BOC is Yarn >>>>>>>>>> NPM😁

Collapse
 
moopet profile image
Ben Sinclair

Hello-world in gatsby is a quarter of a gigabyte. I understand that having to download that again on a metered connection would be annoying, but I think the bigger problem is that HELLO WORLD IS A QUARTER OF A GIGABYTE.

Collapse
 
faraazahmad profile image
Syed Faraaz Ahmad

HELLO WORLD IS A QUARTER OF A GIGABYTE

WHAT?! WHY???!!

Thread Thread
 
moopet profile image
Ben Sinclair

node_modules

Thread Thread
 
x1k profile image
Asaju Enitan

what I can't quite figure out is why don't node_modules work like python modules, just download what is needed. no more

Thread Thread
 
moopet profile image
Ben Sinclair

Partly I think the problem is that when things go wrong, the recommended solution for most problems is to delete node_modules and run setup again.
It's the old "have you tried turning it off and on again?" solution.

Collapse
 
savagepixie profile image
SavagePixie

Maybe you'd like to try pnpm, then. It's big advantage is that it only ever keeps one copy of each package in your computer. So if you're using dependencies that you already use in other projects, you don't need to download them again.

Collapse
 
shelob9 profile image
Josh Pollock

Good point. Worth trying.

I used verdaccio for awhile. It proxies npm registry with a cache.. I was able to run npx create-react-app without an internet connection. I never figured out how to avoid writing localhost urls in my lockfile though.

Thread Thread
 
x1k profile image
Asaju Enitan

maybe it could be in the docs?

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
pavelloz profile image
Paweł Kowalski

Its a shame, because npm is just as fast as yarn nowadays. ;)

Collapse
 
navicsteinr profile image
Navicstein Rotciv

good thoughts about yarn, have your tried pnpm?

Collapse
 
shelob9 profile image
Josh Pollock

Not yet. Have you?

Collapse
 
navicsteinr profile image
Navicstein Rotciv

Yeap, it's really cool only that in some vue projects it breaks HMR due to the symlink

Collapse
 
nurofsun profile image
Nurofsun

Personally, I prefer use pnpm :)

Collapse
 
moonsmile profile image
Nguyễn Minh Tuấn

I use yarn because project doesn't show bugs :V