fbpx

Choosing Between npm and Yarn

By Sewvandi Wickramasinghe

Both npm and yarn are package managers. As an alternative to npm, yarn was built by Facebook. They developed yarn to solve the problems they had when using npm, specifically the issues of accuracy, protection and speed. Within this article we’ll look at npm and yarn differences.

npm, the Node package manager

Npm is the default package manager we all know, and it is bundled with node.js. It is the command-line interface to the npm ecosystem. It is designed for battle-tested, incredibly flexible and uses JavaScript developers every day by hundreds of thousands.

npm install

Using npm -v you will see the current npm version which you are using.

If you have an older version of npm then you can update it to the latest version using the following command.

npm install npm -g

To access npm help, use npm help in the terminal.

npm help

The npm alternative, Yarn package manager

Yarn caches every package that it installs, so that it never has to do so again. This also incorporates activities to optimize resource utilization and installation times are quicker than ever before.

If you have npm installed, you can just install yarn with npm.

npm install yarn --global

What differs yarn from npm?

Yarn is somewhat distinct from npm. The key reason developers choose to move to yarn is its stability. Firstly, yarn caches all the packages installed. Yarn is more convenient than npm. This is because yarn installs that package at once in comparison with npm that installs one at a time.

In npm, the versions of the installed packages can be different when we need to deploy the project on different machines. I assume this was the reason yarn first appeared. Yarn has an engaged community of users.

Both npm and yarn download packages from npm repository. Npm will run code from dependencies automatically. While this feature has its own accessability, it has raised several security issues.

Yarn only installs from your yarn.lock or package.json files. Yarn.lock ensures that all users have the same software installed. It reduces the ability for bugs to be found in different versions.

Any yarn problems?

Yes, unfortunately 🙁

Here are two major issues.

1)Installation of native modules is difficult.
2)Yarn doesn’t work with any node.js version older than 5.

Which one would you select? yarn or npm?

There are many improvements yarn has higher speeds and more control over npm. But many people suggest that if you already know npm, a move to yarn isn’t really necessary. To sum-up personally I will favor to use npm 🙂