DEV Community

What is the difference between yarn and npm these days?

Ari Kalfus on June 16, 2020

Back when yarn was first released, the defining feature was yarn's lockfile, which sped up dependency tree resolution and thus the overall install ...
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.

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
 
adam_cyclones profile image
Adam Crockett πŸŒ€

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

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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
spiritupbro profile image
spiritupbro

how bout pnpm tho?

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€

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