What is the difference between brew, yarn, and npm?

I was using the react-native package which I installed globally with npm. Now it says at the first line after executing the init command. The following:

Installing react-native from npm...
Consider installing yarn to make this faster: https://yarnpkg.com

So I was checking that website and it looked interesting to me, but I don't exactly know what it would be. At first, I thought that I would need brew to install yarn, so I could yarn to install npm. But now I think that yarn is a replacement of npm. Is that a correct statement?

Why would I like to have so many package managers?

I understand that it's useful for software like Atom or Visual Studio Code to have their own package manager. But for development, I do not see the reason why someone would like to use four different package managers (brew for 'primary software', yarn for npm packages, npm for backend modules and bower for front-end libraries). How can this package manager forest be untangled?


I am not familiar with brew, but I suppose you mean the Homebrew software package management system for macOS.

Then the purpose of each system is:

  1. brew: installation of software, i.e. ready to consume applications like wget.
  2. npm: installation of packages (libraries), i.e. pieces of functionality to help you build your own applications.
  3. yarn: also installation of packages.

Yarn has some advantages over npm, the main two are the speed and the predictability. Yarn reuses the npm's package.json file and doesn't change its structure. Therefore you can run yarn install instead of npm install and theoretically everything will work automatically.

P.S. I agree, https://yarnpkg.com doesn't have enough background on why the hell we need another package management system, but there is a great article which fills that gap.


yarn vs npm

yarn and npm are both manage module installations and dependencies. Yarn was built to address some of the shortcomings of npm.

The biggest advantages of yarn over npm are

  1. Installing packages with yarn is parallelized and so package installation is faster.
  2. package.json can be very loose in terms of version numbers. yarn.lock (similar to npm shirkwrap) locks this down so that two machines with the same package.json always install the exact same packages.

  3. yarn allows you to check why some packages are installed (understand the dependency tree)

Ref: https://www.sitepoint.com/yarn-vs-npm/


Yarn is a JavaScript package manager built by Facebook, Google, Exponent, and Tilde. It is created to remove or overcome the features that lack in npm. In comparison with npm it has

  • Enhanced Security
  • Offline mode
  • Parallel Installation - Therefore, faster installation

Another major difference was the yarn.lock file, but after npm ^5.x.x they provide the package-lock.json file too.

And the commands of yarn works like npm:

# Starting a new project
npm init === yarn init

# Installing all the dependencies of the project
npm install === yarn or yarn install

# Adding a dependency
npm install [package] === yarn add [package] # The package is saved to your package.json immediately.
npm install  [package]@[version] === yarn add [package]@[version]
npm install [package]@[tag] === yarn add [package]@[tag]

# Add a dev dependency
npm install [package] --save-dev === yarn add [package] --dev

# Upgrading a dependency
npm update [package] === yarn upgrade [package]
npm update [package]@[version] === yarn upgrade [package]@[version]
npm update [package]@[tag] === yarn upgrade [package]@[tag]

# Removing a dependency
npm uninstall [package] === yarn remove [package]

# View registry information
npm view [package] === yarn info [package]

# List installed packages
npm list === yarn list
npm list --depth === yarn list --depth=0

# Install packages globally
npm install -g [package] === yarn global addb [package]

# Run a defined package script
npm run [script] === yarn run [script]

Refferences

https://www.sitepoint.com/yarn-vs-npm/

https://scotch.io/@brian_kimo/npm-vs-yarn

and the official announcement

https://code.facebook.com/posts/1840075619545360


Yarn is, like NPM, a package manager for Node.JS. Yarn is built by Facebook. It's faster and has more features than NPM.

Their main selling points are:

  • Security With yarn.lock file (similar to NPM's npm-shrinkwrap.json) all dependencies are locked on the exact version. So, you don't have that “But it works on my machine” struggles anymore. Everyone has the same versions locked in yarn.lock file
  • Speed Yarn uses (fast) proxies and (offline) caching to deliver your modules faster. It also has a LICENSE checker, which checks the license of all your dependency modules.