Providing a clear introduction for those starting out.
npm, established in 2010, has long been the go-to package manager for JavaScript developers. To initiate dependency management on your project, simply enter the command:
npm init
This action will create a package.json
file housing all of the project's dependencies.
Following that step,
npm install
will establish a directory called node_modules
and fetch the specified dependencies (listed in the package.json
file).
In addition, it will generate a package-lock.json
file outlining the dependency tree that was built. This document allows developers to replicate the exact set of dependencies installed. For instance, one developer might incrementally upgrade a dependency from v2 to v3, while another chooses to jump directly to v3.
While installing dependencies, npm lacks determinism, potentially resulting in distinct node_modules
directories between developers and leading to variances in behavior. ** npm faced backlash due to incidents like the one in February 2018 when version 5.7.0 resulted in altering system files if sudo npm was run on Linux.
To address such issues among others, Facebook introduced a new package manager in 2016 called Yarn, a faster, more secure, and reliable option for JavaScript packages.
You can introduce Yarn to your project by executing:
yarn init
This process generates a package.json
file. Subsequently, you can install the dependencies with:
yarn install
A directory named node_modules
will be created. Alongside this, Yarn will craft a file titled yarn.lock
, serving a similar purpose as package-lock.json
, yet constructed using a deterministic algorithm for consistent builds.
If you initially began your project with npm, transitioning to Yarn is seamless as it leverages the same package.json
. Refer to Migrating from npm for further guidance.
Despite advancements made in each subsequent release of npm, some projects still opt for npm over Yarn.