Why Are You Still Using Yarn in 2018?

Matt Turnbull
ITNEXT
Published in
5 min readJul 4, 2018

--

I remember when Yarn exploded onto the scene.

It was super fast. It saved minutes with every yarn install.

It ensured your /node_modules exactly matched the /node_modules of your buddy next to you. And the /node_modules on the server too. No more “it works for me ¯\_(ツ)_/¯”.

And, emojis everywhere! 🔥🔥🔥

6 months passed…

And npm made an epic comeback with v5, bringing speed improvements and consistent /node_modules. Yep, all those reasons we switched to Yarn in the first place.

Many devs are now wondering:

“Is Yarn still necessary?”

I think it’s time to switch back to good ol’ npm. Let’s take a look.

npm is just as fast as Yarn.

See for yourself:

# Backup
mv node_modules node_modules_backup
mv package-lock.json package-lock.backup.json
mv yarn.lock yarn.backup.lock

# Test cold npm speed
time npm install
# Reset modules
rm -Rf node_modules
# Test warm npm speed
time npm install
# Test cold yarn speed
time yarn install

# Reset modules
rm -Rf node_modules
# Test warm yarn speed
time yarn install
# Reset
rm package-lock.json
rm yarn.lock

# Restore
mv node_modules_backup node_modules
mv package-lock.backup.json package-lock.json
mv yarn.backup.lock yarn.lock

I ran this three times, and speeds were very similar.

You can easily switch npm versions.

Bob has Yarn v1.1 installed, and Brenda has Yarn v1.2. As they install and remove dependencies on a project, Yarn writes to a yarn.lock file. But the lock file format is slightly different between Yarn v1.1 and Yarn v1.2. Not fun for Bob and Brenda.

You can specify which Yarn version(s) are compatible with your project using engines in package.json.

But what if you need to shift between projects regularly, each requiring different Yarn versions?

You need to stop using brew (brew uninstall yarn) and revert back to an npm install. Run npm install yarn@1.1 --global and npm install yarn@1.2 --global as you flip between projects. Yep, re-installing Yarn in its entirety every single time you switch projects. What a nightmare!

But it’s super easy with npm!

Use nvm or n and switch versions instantly with one command. Boom.

Use Lerna to manage your workspaces.

If you’re using Yarn for the workspaces feature, consider using Lerna instead. It does everything Yarn offers with additional features, eg: managing workspace versions, running commands within each workspace, and publishing workspaces.

Use npm-check to upgrade interactively.

The yarn upgrade-interactive command is awesome:

The popular npm-check module does the same thing:

And is very easy to setup:

npm install npm-check --save-dev

Add a script to package.json:

{
"scripts": {
"upgrade-interactive": "npm-check --update"
}
}

Then use npm run upgrade-interactive.

Yarn complicates.

From the Jest readme:

You don’t need Yarn to use Jest. But here it is, pushed on you. Now I need to convert yarn test into an npm command… ok, is it npm test or npm run test? I can’t quite remember. This is what the readme should tell me!

Even if the readme shows both yarn and npm commands, it unnecessarily adds to the noise. For example, here is the readme for create-react-app:

What benefit is Yarn bringing here? None.

It’s confusing for brand new React developers who are already overwhelmed with learning a new framework. “Which command do I run? What is this Yarn one? Do I need it? Argh!”

I’ve also seen tooling and CI code check for the yarn.lock file, and run a different set of commands if detected.

Choosing to use Yarn on a project means the rest of your dev team, including future contributors, must also use Yarn (to maintain the yarn.lock file).

The JavaScript ecosystem is complex enough. Do we really need Yarn in the mix too?

I wish the Yarn team would contribute their improvements into npm itself to simplify our lives.

npm rocks!

After switching back to npm, I realised my shell setup autocompletes npm run commands by scanning package.json scripts:

Sure, Yarn might eventually have this support (and possibly already does). But npm has the advantage of years and years of community support and tooling.

npm are also bringing out some killer features. Run npm audit to scan your project for vulnerabilities. Use npx to run one-off commands (eg: npx create-react-app instead of installing create-react-app globally).

Give npm another shot.

It’s fast. It’s battle-tested. It’s regularly updated. It has years of community support and tooling (like nvm, n, and the shell autocomplete above). Popular modules like Lerna and npm-check achieve Yarn’s extra features.

If you’re writing javascript then you already have npm installed. Simplify your dev tools and use npm again. And if you miss Yarn, let me know why in the comments below!

It’s impossible to keep up with JavaScript.

You’re catching up every chance you get. Scrolling… reading… refreshing… skimming. You’re lost in 42 browser tabs of articles, tutorials, and GitHub repos. You bookmark a handful to check out later (or, never).

It’s overwhelming. There’s too much to know.

So I developed a system that easily keeps me up-to-date.

--

--