Lorenzo Bianchi Logo

npm vs yarn vs pnpm Cheat Sheet

Author

Lorenzo Bianchi

Date Published

npm, yarn, and pnpm

Guide to JavaScript Package Managers: npm, yarn, and pnpm

Introduction

npm, yarn, and pnpm are the three main package managers for JavaScript and Node.js. Each of them allows you to install, manage, and update your project dependencies, but with slightly different approaches and philosophies.

npm is installed by default when we install Node.js on our system.

yarn, initially developed by Facebook, was created as an alternative to npm to offer greater speed and reliability.

pnpm (performant npm) is a more recent package manager that focuses on disk space efficiency and speed, using an innovative approach to module management.

Installation

npm

No installation required, it's already included with Node.js.

yarn

To install yarn globally:

npm install -g yarn

Currently, two main versions are available: yarn v1 (classic) and yarn v2+ (modern with advanced features). Check the official yarn documentation for details on migrating between versions.

pnpm

To install pnpm globally:

npm install -g pnpm

Alternatively, you can use curl on macOS or Linux:

curl -fsSL https://get.pnpm.io/install.sh | sh -

Advantages and Features

npm

  • Official manager for Node.js
  • Large community and universal compatibility
  • Simple and intuitive package management
  • Continuous improvements in recent versions

yarn

  • Superior performance thanks to caching
  • Deterministic locking with yarn.lock file
  • Workspaces for monorepo management
  • More intuitive commands compared to npm

pnpm

  • Space efficiency: uses a "content-addressable" approach to avoid duplicates
  • Speed: faster than npm and yarn in many scenarios
  • Strict integrity: prevents unauthorized access to undeclared dependencies
  • Native monorepo support: built-in workspaces support
  • Lower disk consumption: drastically reduces space occupied by node_modules

Main Commands

Commandnpmyarnpnpm
Install dependenciesnpm installyarn installpnpm install
Add packagenpm install [package]yarn add [package]pnpm add [package]
Add dev dependencynpm install --save-dev [package]yarn add [package] --devpnpm add --save-dev [package]
Add optional dependencynpm install --save-optional [package]yarn add [package] --optionalpnpm add --optional [package]
Install specific versionnpm install [package]@1.2.3yarn add [package]@1.2.3pnpm add [package]@1.2.3
Remove packagenpm uninstall [package]yarn remove [package]pnpm remove [package]
Update dependenciesnpm updateyarn upgradepnpm update
Update specific packagenpm update [package]yarn upgrade [package]pnpm update [package]
Global installnpm install -g [package]yarn global add [package]pnpm add -g [package]
Global uninstallnpm uninstall -g [package]yarn global remove [package]pnpm remove -g [package]
Run testsnpm testyarn testpnpm test
Run scriptsnpm run [script]yarn [script]pnpm run [script]
Check outdated packagesnpm outdatedyarn outdatedpnpm outdated
Clean cachenpm cache clean --forceyarn cache cleanpnpm store prune
Rebuild packagesnpm rebuildyarn add --forcepnpm rebuild
Loginnpm loginyarn loginpnpm login


Monorepo and Workspaces Management

All three managers support workspaces for managing monorepo projects, but with slightly different syntax.


npm: Configuration in package.json

1{
2 "workspaces": [
3 "packages/*"
4 ]
5}
6


yarn: Configuration in package.json or .yarnrc.yml


1{
2 "workspaces": [
3 "packages/*"
4 ]
5}


pnpm: Configuration in pnpm-workspace.yaml

1packages:
2 - 'packages/*'



Lock Files

All three managers create lock files to ensure reproducibility:

  • npm: package-lock.json
  • yarn: yarn.lock
  • pnpm: pnpm-lock.yaml

It's important to commit these files to version control to ensure that all developers and CI/CD systems use the same versions.


Official Resources and Documentation