Major Differences Between Yarn & NPM
- NPM was developed as an open source project in 2009. Yarn was released by Facebook in 2016 as an improvement upon the foundation that NPM laid.
- Yarn uses
yarn addwhile NPM uses
npm install(Can be confusing when switching between the two.)
- Yarn keeps a copy of packages you download stored locally. I'll explain why in a little bit.
- Both Yarn and NPM use the
package.jsonfile to get the packages to install. However, Yarn uses
yarn.lockand NPM uses
package-lock.jsonto more explicitly state which package version to get.
A Breif History of NPM
Yarn Module Cache
Every time you install a new package with Yarn, it stores a copy of it locally on your computer. This way when multiple projects require the same package, Yarn doesn't have to go download the required package again, it just grabs it off your hard drive and puts it in the project you're installing, saving you time and bandwidth.
Blazing Saddles: Package Manager Boogalo
According to a test done by GitHub user appleboy that you can reproduce yourself, Yarn both with and without it's cache is significantly faster at installing modules, and even installs without internet! (assuming you've cached the package you're installing).
Here are the results of their test comparing NPM to Yarn:
|Test||npm install||npm ci||yarn|
|install without cache (without node_modules)||3m||3m||1m|
|install with cache (without node_modules)||1m||18s||30s|
|install with cache (with node_modules)||54s||21s||2s|
|install without internet (with node_modules)||-||-||2s|
Blazing Fast! Even without using a cache, Yarn is 200% faster than NPM.