Node.js Course
Node.js
/
Intermediate

npm vs Yarn vs pnpm

Definition

A comparison of the three major package managers in the Node.js ecosystem used to install and manage dependencies.

Explain Like I'm New

NPM is the default package manager that comes with Node. Yarn was created by Facebook to be faster and more secure than NPM (though NPM caught up). PNPM is the newest and fastest; instead of copying heavy `node_modules` folders into every single project, it saves one copy centrally on your computer and links to it, saving massive amounts of hard drive space.

Real World Example

If you have 10 React projects, NPM and Yarn will download the 300MB `react-scripts` folder 10 times (using 3GB of space). PNPM downloads it once, and creates a fast link to it in all 10 projects.

Common Use Cases

  • •Speeding up CI/CD pipelines
  • •Saving local disk space
  • •Monorepo management

Terminal Output

bash / terminal
// --- COMMON COMMAND EQUIVALENTS --- // 1. Installing a project // npm install // yarn install // pnpm install // 2. Adding a specific package // npm install axios // yarn add axios // pnpm add axios // 3. Removing a package // npm uninstall axios // yarn remove axios // pnpm remove axios // 4. Running a custom script (e.g., 'start' in package.json) // npm run start // yarn start // pnpm start console.log("All three accomplish the exact same goal, but pnpm is currently the fastest and most efficient!");

Interview Questions

basic

  • Which package manager comes pre-installed with Node.js?

intermediate

  • How does pnpm save so much disk space?

advanced

  • What is a Monorepo workspace?

Flash Cards

Question

Which comes pre-installed?

Click to reveal answer
Answer

npm (Node Package Manager).

Question

How does pnpm save space?

Click to reveal answer
Answer

Using a global content-addressable store and hard links. It stores a single copy of a package globally on your hard drive, and uses OS-level symlinks to trick your project into thinking it has its own copy of `node_modules`.