DEV Community

Cover image for The Difference Between NPM and Yarn
Samitha Wijesekara
Samitha Wijesekara

Posted on • Edited on

The Difference Between NPM and Yarn

What is Package Manager?

A package manager is a tool to create project environments and easily import external dependencies. By using a package manager we could able to automates the process of installing, upgrading, configuring, and removing the dependencies from the project environment.

What is NPM?

NPM is commonly known as node package manager, maintained by NPM, Inc. NPM is the popular package manager among JavaScript developers. It is the default package that is automatically installed whenever we install Node.js on our system. (https://www.npmjs.com/)

What is Yarn?

Yarn package manager developed in 2016 by Facebook. It is a another package manager for the JavaScript programing language. Yarn provides speed, consistency, stability, and security as an alternative to NPM.(https://yarnpkg.com/)


Speed

The main difference between NPM and Yarn is the package installation process. Yarn installs packages in parallel. Yarn is optimized to fetch and install multiple packages at once.

NPM will perform a serial installation process. It install every package independently.

So in this case Yarn has a speed installation process than NPM.

Security

NPM package manager has perform a security check on each install. Yarn checks behind the scenes to ensure you're not downloading any rogue scripts and other files that can conflict with your project's dependencies. Security is one of Yarn’s core features.

Ease of use

NPM and Yarn both package managers are user-friendly and have a good user experience.


Basic Commands

To see list of commands:
NPM - npm
Yarn - yarn

Install dependencies from package.json:
NPM - npm install
Yarn - yarn

Install a package and add to package.json:
NPM - npm install package --save
Yarn - yarn add package

Install a devDependency:
NPM - npm install package --save-dev
Yarn - yarn add package --dev

Remove a dependency:
NPM - npm uninstall package --save
Yarn - yarn remove package

Upgrade a package to its latest version:
NPM - npm update --save
Yarn - yarn upgrade

Install a package globally:
NPM - npm install package -g
Yarn - yarn global add package

Top comments (9)

Collapse
 
lyrod profile image
Lyrod

Since npm 5, --save is not needed. npm i package. Or dev npm i -D package.

npm remove works too.

You should check for pnpm.

Collapse
 
lico profile image
SeongKuk Han

So, is yarn faster than npm in most cases? I wonder what advantages the serial installation process has. why does npm adopt it?

Collapse
 
patricknelson profile image
Patrick Nelson • Edited

Not sure. I just did a test between npm, yarn and pnpm and it seems that even though pnpm is still lighter weight and generally faster than npm, yarn is reliably fast (but varies depending on the versions of everything).

I'm leaving my benchmarks below but I realized partly why npm wasn't as fast was because I was on an older node version (so, npm version 6.14.16 in my case with yarn 1.22.17). You should definitely checkout this post for a far better breakdown though: blog.logrocket.com/javascript-pack...


For example, I got the following results testing each one (note that this was just a single benchmark for each package manager) so it's fairly anecdotal, however it's fairly typical of my experience.

Fresh install: (no caches)

  • npm 1m 23s
  • pnpm 50s
  • yarn 41s

Fresh install: (with caches)

  • npm 1m 7s
  • pnpm 32s
  • yarn 15s
Collapse
 
spicylemonhaha profile image
spicylemonhaha

Since parallel downloads are faster than serial downloads, why hasn't npm changed the way that download dependencies after multiple iterations

Collapse
 
spicylemonhaha profile image
spicylemonhaha
Thread Thread
 
spicylemonhaha profile image
spicylemonhaha

59f86ef90 43be9d222 e906cdd98 #16633 npm now parallelizes tarball extraction across multiple child process workers. This can significantly speed up installations, specially when installing from cache, and will improve with number of processors. (@zkat)

Does npm indicate in this release note that it already uses parallel downloads

Collapse
 
anthonyjdella profile image
Anthony

If Yarn is faster than NPM and they both have similar security checks, why isn't Yarn more popular?

Which package manager has more packages? Which has the most daily installs?

Collapse
 
ezpzdevelopement profile image
Ez Pz Developement

nice post thank you ...

Collapse
 
samithawijesekara profile image
Samitha Wijesekara

Thank you for sharing this. @skozeniuk