DEV Community

Ari Kalfus
Ari Kalfus

Posted on

What is the difference between yarn and npm these days?

Back when yarn was first released, the defining feature was yarn's lockfile, which sped up dependency tree resolution and thus the overall install of a project. Well, npm 5 came out with a lockfile a few months later. I don't know if it's anything new, but I've noticed in the last several months that both large community projects, enterprise blog articles, and individual stuff on platforms like dev.to always put the yarn commands first with npm commands as a secondary line, or a comment underneath the yarn command.

Why? Why do people still use yarn? Why does it seem like yarn is preferred these days? What differentiators does it actually have against npm 6.x?

Latest comments (19)

Collapse
 
spiritupbro profile image
spiritupbro

how bout pnpm tho?

Collapse
 
pavel910 profile image
Pavel Denisjuk

For us at Webiny, NPM is a no-go because it doesn't support workspaces.

You could say "use Lerna". Yes, we do use Lerna, but its hoisting capabilities and speed is far behind Yarn. We have 70+ packages in our monorepo, and Lerna just can't handle that with all the hoisting going on. And the results of hoisting between Yarn and Lerna are completely different.

For those who care to go into a deep analysis, there is a great article which compares npm, yarn, lerna and all the combinations of those, with benchmarks, etc.: doppelmutzi.github.io/monorepo-ler...

Collapse
 
rubiin profile image
Rubin • Edited

I love yarn because it does parallel downloads making its 2-3 times faster than npm. Also you can run scripts easily with yarn script-name . I know the second one is not a huge thing but who wouldn't want to save some hundred keystrokes per day

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

I am going to add something, I prefer cdns because esmodules but I also prefer pika cdn

Collapse
 
danku profile image
Daniel McMahon

I recently bricked the npm setup on my windows machine, immediately installed yarn and just continued coding. Being versatile enough to use both is fantastic!

I have found yarn to generally be faster in personal projects but haven't done the proper benchmarking to confirm!

Collapse
 
baso53 profile image
Sebastijan Grabar

It's a My dad is stronger than your dad discussion.

I pick Yarn, because it installs dependencies faster and I can use the yarn command-line tool to execute both dependencies and npm scripts. I frequently mess up npm/npx/npm run

Collapse
 
shinesanthosh profile image
Shine Santhosh

That's a good comparison😂

Collapse
 
lenaggar profile image
Mahmoud ElNaggar • Edited
  • Yarn is faster
  • Logs are cleaner for me (most of the time if something went wrong I know what happened right away, I don't need to squint my eyes and go through all the lines to inspect which line exactly is relevant to what I need to do)
Collapse
 
guitarino profile image
Kirill Shestakov

I'm really liking yarn pnp. The idea that you don't have to do npm install or think at all about node_modules is awesome. Another great thing is that all your dependencies, as well as yarn itself, are stored inside your repo. I like the direction that yarn takes you into. It takes you away from reliance on npm, and allows you to be self-sufficient.

Collapse
 
artis3n profile image
Ari Kalfus

pnp is new to me... will have to see how it works. It looks like its resolution gets funky with typescript.

Collapse
 
guitarino profile image
Kirill Shestakov

I've been using TypeScript without much problem. If you're referring to TS in VSCode, you can use a custom workspace TS version (and pnpfy it) for the imports to get resolved properly.

Collapse
 
shinesanthosh profile image
Shine Santhosh • Edited

try
yarn add express
and
npm i express
You can see the difference by yourself. Yes, I love it because it's faster but also it's easier to run scripts with yarn. You don't have to
npm run dev
just
yarn dev

I'm lazy; I hate to type another 3 letter word😅

But having different lockfiles can put you in a lot of dependency issues.
So if I clone a project I'll look for the lockfile. And will use yarn and npm depending upon it.

So the thing is, yarn can install stuff faster.

Collapse
 
artis3n profile image
Ari Kalfus

Maybe time for me to go back to yarn, then!

Collapse
 
alaindet profile image
Alain D'Ettorre

"NPM is by far the most popular so why switch to yarn" is what 90% people would say in my opinion

Collapse
 
artis3n profile image
Ari Kalfus

That's kind of why I posed this question. I used yarn initially for the lockfile and moved back to npm when it added a lockfile. Want to understand why people still use yarn.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

Yarn is still faster that's all I can say.

Collapse
 
flashblaze profile image
Neeraj Lagwankar

From my own personal experience, I'm going to start using yarn.
I started using yarn due to ReactNative and was blown away by the install speeds. It was easily 2-3 times faster than npm. I haven't tested it for other than React or ReactNative but I think I'll definitely be using it a lot more in the near future.

Collapse
 
chicojasl profile image
Justo

Hi Neerja. I can tell yarn is faster. Recently I used npm for a fresh React Native project and... slower.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

I now prefer NPM, but NPM does sometimes have problem with installing from package-lock.json.

Some other still missing features of NPM include,

Luckily, I now know an easy way to npm-not-yarn and yarn-not-npm, with zero dependency.

echo 'engine-strict=true' >> .npmrc
// package.json

{
    "engines": {
      "node": "12.x",
      "npm": "6.x",
      "yarn": "please-use-npm"
    }
}

It is a best idea to pin dependencies and engines visibly for open source contributors, anyway.