What are the differences between npm and Yarn?
I am thinking of migrating from npm to Yarn, can anyone tell me what the main differences are?
Is there any considerable benefit between the two?
2 answers
Yarn and NPM are package managers, which basically fulfill the same mission.
Yarn was born within facebook and due to some frustration that NPM is slowly iterating and getting slow. This has been corrected, perhaps by yarn concurrency influence.
For a more exhaustive comparison we would have to compare version to version of npm and Yarn. But they basically do the same, and (for now) are compatible since they both use package.json
as source of information what packages and versions the project needs. But beware: as both write (and delete) packages/programs from the node_modules
Directory it may happen that one installs/deletes specific versions that the other installed/removed.
Differences in API:
NPM | YARN
npm init | yarn init
npm install ... | yarn add ...
npm update ... | yarn upgrade ...
npm remove ... | yarn remove ...
Some points that I consider important.
Determinism
In the Node ecosystem, dependencies are placed within a directory named node_modules
in your project. However, this file structure may be different from the actual dependency tree because duplicate dependencies are merged. The client npm
installs dependencies in the directory node_modules
in a non-deterministic manner . This means that, based on the order in which the dependencies are installed, the structure of a directory node_modules
can be different from one person to another. These differences can cause "works on the machine" errors that take a long time to discover.
With yarn
, you always know that you are getting the same content on each development machine. yarn
uses lockfiles and an installation algorithm that is deterministic and reliable. These lockfiles lock dependencies installed in a specific version, ensuring that each installation results in the same file structure in node_modules
on all machines.
Parallelization
yarn
it is able to parallelize operations, maximizing resource utilization and making the installation process faster.
Resources
The network is used more efficiently by yarn
. There is a global caching of each package that is downloaded, so you only download each package once. In addition, it is possible that it will better use of other system resources (such as RAM).
Sources