Tuesday, November 23, 2021

Have You Switched From NPM To Yarn? NPM Vs Yarn

 

Have You Switched From NPM To Yarn? NPM Vs Yarn

Have You Switched From NPM To Yarn NPM Vs Yarn

Node.js is best for building highly scalable data-intensive and real-time backend services that power client applications. It allows you to create dynamic web pages written in Javascript such as video streaming sites, single page apps, online chatting applications and so on. These pages are executed on the server before being sent to the browser. NPM (Node Package Manager) is very popular among javascript programmers.  But it started facing an issue with performance and security, and this makes the package manager unreliable. That’s when Yarn was born. It has been gaining popularity since its inception. What do you think? Does Yarn replace NPM? Before digging into it, let’s have a look at an overview of NPM and Yarn.

What Is NPM?

NPM logo

Node package manager is known as NPM. It is a default package manage in Node.js and popular among the Javascript programmers community since its inception in 2010. It automatically comes with Node.js on your system and brings three important components- command line interface, online database of enumerable packages called as npm repository and website to manage different aspects of NPM experience. Over the years, NPM has gained tremendous popularity and now has a huge community of programmers that makes it easy to find help. 

What Is Yarn?

Yarn logo

Yarn, released by Facebook in 2016, is a popular package manager for javascript programming language. One of the main intentions to create Yarn was to address some of the security and performance shortcomings of working with npm. It provides similar functionalities as NPM. Although it has a slightly different installation process, it enables you to access same registry. So switching from NPM to Yarn is hassle-free.  

NPM Vs Yarn- The Difference

1. Installation-

NPM-

It is distributed with Node.js so when you download Node.js, automatically you will have npm installed and ready to use. Once the Node.js has been installed, use the command to ensure installation was successful- 

node -v
npm -v

Yarn-

For Yarn, you have two options.  If you want to install Yarn using npm, enter the following command-

npm install yarn --global

But, programmers advise against using npm to install Yarn. Then, a better alternative is to install Yarn using your native OS package manager. For instance, if you’re using brew on Mac, you’d enter:

brew update
brew install yarn

If you’d like to try Yarn on an existing npm project, run –

yarn

Then see your node_modules folder displayed using Yarn’s resolution algorithm.

2. Installing Project Dependencies-

Let’s see how project dependencies are installed. When you run npm install, dependencies  are installed sequentially. The output logs in the terminal are informative but a bit hard to read.

To install packages with Yarn, you run the yarn command. Yarn installs packages in parallel that is the reason for being quicker than npm. If you’re using Yarn 1, you’ll see that yarn logs of yarn output are clean, visually distinguishable and brief. Also, they’re ordered in a tree form for easy comprehension. But it is changed in version 2 and 3, where logs aren’t human readable and intuitive. 

We’ve seen that npm and Yarn have different commands to install packages.

3. Speed And Performance-

Whenever Yarn or npm need to install package, they carry out a series of tasks. In npm these tasks are executed per package and sequentially, meaning it will wait for the package to be fully installed before moving to the next. Conversely, Yarn executes these tasks in parallel and this improves the performance. 

Both managers offer caching mechanisms, Yarn seems to do it a bit better. Implementing a zero-install paradigm, it is capable of installing packages almost in zero time. It caches each package and saves it on the disk. Hence in the next installation of this package you don’t need to have an internet connection as the package is installed offline from the disk.

Though Yarn has some advantages, the speeds of npm and Yarn, in the latest version, are pretty comparable. Hence, we can’t decide the final winner.

Read More


No comments:

Post a Comment