Why I Use Yarn, Not npm
Tags: javascript, npm, node
Published: 2020-01-18 16:16:48

*Update: This post is based on using Yarn 1. A major update [Yarn 2](https://dev.to/arcanis/introducing-yarn-2-4eh1) has been released. I use Yarn 1.x.*

In the comments of [my post about things I do everytime I start a Gatsby site](https://dev.to/shelob9/a-few-things-i-do-every-time-i-start-a-new-gatsby-site-5gd), [there was a really good comment](https://dev.to/wezpyke/comment/kdd8) 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](https://yarnpkg.com/lang/en/docs/install/#windows-stable) or [pnpm](https://pnpm.js.org/).


## 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](https://www.npmjs.com/about). Last year they braught in a new CEO and [laid off a several long-term employees to stop union organizing](https://www.theregister.co.uk/2019/04/01/npm_layoff_staff/), 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:

```bash
npm run build
```

Or with Yarn:

```bash
yarn build
```

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](
https://yarnpkg.com/lang/en/docs/cli/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:

```bash
yarn upgrade-interactive --latest
```

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](https://yarnpkg.com/lang/en/docs/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](https://yarnpkg.com/lang/en/docs/cli/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](https://twitter.com/josh412).