NPM VS Yarn

0
526

Introduction.

NPM(Node Package Manger) and Yarn are both node package managers that allow developers to create, use, reuse and distribute/share packages with the rest of the world. Yarn is an allternative package manager for the node built in NPM. In this article, we focus on some comparisons between NPM and Yarn in both their performance and commands used.

#Installing Yarn

Yarn can be installed in either of two ways. Using npm :

npm install yarn --global

Or using the native os package manager. For instance using brew on Mac OS.

brew update
brew install yarn

#Performance Comparison

Artberri developed a tool call npm-yarn-benchmark that compares the performance of npm and yarn by making the two tools install 3 libraries reactember and angilar2 and comparing the time taken. It will also perform installations with precached packages and with empty cache to see the difference between both situations. The following table shows circle CI results after installation.

Advantages of Yarn

Security

Yarn uses checksums to verify the integrity of every installed package before its code is executed.

Offline Mode

If you’ve installed a package before, you can install it again without any internet connection. Packages installed using Yarn with yarn add packagename are stored on your disk so that during the next install, this package will be used instead of sending a HTTP request to download the package again from the registry.

Deterministic

The same dependencies will be installed the same exact way across every machine regardless of install order. Yarn resolves works on my machineissues around versioning and non-determinism by using lockfiles and an install algorithm that is deterministic and reliable. These lockfiles lock the installed dependencies to a specific version, and ensure that every install results in the exact same file structure in node_modules across all machines. The written lockfile uses a concise format with ordered keys to ensure that changes are minimal and review is simple.

Network Performance

Yarn efficiently queues up requests and avoids request waterfalls in order to maximize network utilization.

Same Packages

Install any package from npm and keep your package workflow the same.

Network Resilience

A single request failing won’t cause an install to fail. Requests are retried upon failure.

Flat Mode

Resolve mismatching versions of dependencies to a single version to avoid creating duplicates.

Parallel Installation

Whenever npm or Yarn needs to install a package, it carries out a series of tasks. In npm, these tasks are executed per package and sequentially, meaning it will wait for a package to be fully installed before moving on to the next. Yarn executes these tasks in parallel, increasing performance.

Usage

We illustrate the usage of several common commands and compare how they are used both in NPM and Yarn.The packages are the same as on the NPM registry. Yarn is basically a new installer, where NPM structure and registry is the same.

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

 #Installing all the dependencies of 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]

LEAVE A REPLY

Please enter your comment!
Please enter your name here