Yarn and pnpm: Alternative package managers

Compare Yarn and pnpm with NPM, learn their basic commands and use cases, and choose a package manager for your project.

🎯 Introduction

In this article, you will learn about Yarn and pnpm, two common alternatives to NPM in frontend development. We will explore the problems they address and how to choose between them for different projects.


🧱 Why use another package manager?

NPM comes with Node.js and has a mature ecosystem and a comprehensive feature set. As projects and dependency lists grew, however, developers encountered several challenges:

  • Slow installations: early NPM versions downloaded dependencies sequentially, making large projects slow to install.
  • High disk usage: each project's node_modules keeps its own copies of packages, duplicating many files.
  • Inconsistent dependency trees: running npm install at different times can produce slightly different trees.

Yarn and pnpm emerged as alternatives to address these problems. They use the same package registry as NPM (the npm registry), so the packages come from the same source; installation and management differ.


✨ Yarn

Yarn is a package manager introduced by Facebook in 2016 to address NPM's slow and unreliable installations at the time.

Key features

  • Parallel downloads: downloads multiple packages at once, making it much faster than early NPM versions.
  • The yarn.lock lockfile: locks exact dependency versions for consistent installations across the team.
  • Offline mode: caches downloaded packages so they can be reused without a network connection.
  • Workspaces: simplify managing a monorepo, where one repository contains multiple packages.

Common commands

# Install all project dependencies
yarn install

# Install a specific package
yarn add dayjs
yarn add dayjs@1.11.13

# Install a development dependency
yarn add eslint -D

# Remove a package
yarn remove dayjs

# Run scripts
yarn dev
yarn build
Yarn commands are shorter than their NPM equivalents: yarn dev corresponds to npm run dev, and yarn add corresponds to npm install for adding packages.

⚡ pnpm

pnpm is a popular package manager whose name stands for performant npm. It optimizes installation speed, disk usage, and dependency isolation.

Key features

  • Content-addressable storage: keeps shared package files on your computer and uses hard links across projects, greatly reducing disk usage.
  • Non-flat node_modules: uses a stricter dependency tree by default to prevent phantom dependencies.
  • Fast installations: extensive reuse of local cached files makes repeated installations very fast.
  • Native workspace support: provides built-in management of multiple packages in a monorepo.

Common commands

# Install all project dependencies
pnpm install

# Install a specific package
pnpm add dayjs
pnpm add dayjs@1.11.13

# Install a development dependency
pnpm add eslint -D

# Remove a package
pnpm remove dayjs

# Run scripts
pnpm dev
pnpm build

🪤 What are phantom dependencies?

A phantom dependency is a package your code directly imports with import or require even though it is not declared in your package.json. It happens to be available because another dependency brought it in.

An example

Suppose your project explicitly installs only element-plus:

package.json
{
    "dependencies": {
        "element-plus": "^2.9.0"
    }
}

Now suppose element-plus depends on vue and @popperjs/core. With a flat node_modules layout, as commonly used by NPM or Yarn, these transitive dependencies can be hoisted to the top level:

node_modules/
├─ element-plus/
├─ vue/          ← Hoisted to the top level
└─ @popperjs/core/  ← Also hoisted

Your code can then run successfully with an import like this:

main.js
import { createApp } from 'vue'; // ⚠️ vue is not declared in package.json!

Here is the problem:

  1. Your package.json does not declare vue, but your code relies on it.
  2. If an element-plus update removes its dependency on vue or changes its version, your code can fail.
  3. A different computer or package manager may hoist dependencies differently, producing “it works on my machine” failures.

This hidden, unmanaged dependency is called a phantom dependency.


🛡 How pnpm prevents phantom dependencies

By default, pnpm uses a non-flat node_modules layout, exposing only packages explicitly declared in your package.json at the top level:

node_modules/
├─ element-plus/  ← Only your explicitly installed packages
└─ .pnpm/         ← Other dependencies are isolated here
    ├─ vue@3.x/
    └─ @popperjs/core@x.x/

With this layout, trying to import vue fails with a module-not-found error because it is not exposed at the top level.

pnpm manages dependencies strictly by default. If package A depends on package B, your application cannot directly import B unless it also declares B. This design prevents many implicit dependency problems.

To use vue in your project, install it explicitly:

pnpm add vue

This adds vue to package.json, making the dependency explicit and manageable instead of relying on a phantom dependency.


🧰 What are workspaces and monorepos?

A monorepo keeps multiple related projects or packages in a single Git repository. A component library repository might contain:

  • packages/ui: UI component source code
  • packages/utils: utility functions
  • apps/docs: a documentation site

Workspaces are a package manager's support for managing multiple packages together. With workspaces enabled, one installation at the repository root manages dependencies for all member packages, which can also reference one another.

A pnpm workspace example

Create pnpm-workspace.yaml in the repository root:

pnpm-workspace.yaml
packages:
    - 'packages/*'
    - 'apps/*'

Then run:

pnpm install

pnpm discovers the projects under packages and apps and installs their dependencies together. Its workspace support integrates with its storage model to provide fast installations and low disk usage compared with Yarn.

You do not need workspaces for a single frontend application. They become useful when you maintain a component library, utility libraries, or several interdependent projects.

⚖️ Comparing NPM, Yarn, and pnpm

FeatureNPMYarnpnpm
Maintainer / originBundled with Node.jsFacebook (now Meta)Open-source community
Installation speedFairly fast (significantly improved since v5)FastVery fast
Disk usageHighHighLow (content-addressable storage)
Lockfilepackage-lock.jsonyarn.lockpnpm-lock.yaml
Dependency layoutFlatFlatNon-flat and stricter
Phantom dependenciesCan occur easilyCan occur easilyLargely prevented
WorkspacesSupportedSupportedNative support with efficient storage
Typical use casesGeneral use; bundled with Node.jsLarge projects and monoreposFast installations, strict dependency management, and monorepos
All three tools access the same NPM registry, so the packages themselves are the same. Your choice mainly depends on team conventions, project size, and how strictly you want to manage dependencies.

💡 How to choose

Choose NPM when

  • You are a beginner and do not want to install another tool.
  • The project is small enough that NPM meets your needs.
  • Your team already uses NPM and wants to stay consistent.

Choose Yarn when

  • The project has many dependencies and you want faster installations.
  • Your team already uses Yarn and wants to stay consistent.
  • You need Yarn workspaces to manage a monorepo.

Choose pnpm when

  • Disk space is limited and you want to reduce usage.
  • The project has complex dependencies and you want to avoid phantom dependencies.
  • Your team values faster installations and stricter dependency management.
Use one package manager per project. Mixing package-lock.json, yarn.lock, and pnpm-lock.yaml can lead to inconsistent dependencies.

🛠 Switch package managers in a project

If you pick up a pnpm project but do not have pnpm installed, install it globally first:

npm install pnpm -g

This course uses pnpm 10.6.2. Specify the version to match the course environment:

npm install pnpm@10.6.2 -g

After installation, check the version with pnpm -v, then use pnpm install to install the project's dependencies.


❓ Questions and answers

Q1: Can Yarn and pnpm install packages from the NPM registry?

Yes. Both are compatible with the NPM registry and use the same package source.

Q2: Should I commit yarn.lock and pnpm-lock.yaml to Git?

Yes. Like package-lock.json, they lock dependency versions for consistent installations.

Q3: How does pnpm save disk space?

pnpm stores downloaded package files in a shared store. Projects reuse those files through hard links rather than keeping separate copies.

Q4: What is a phantom dependency?

It is a package your code uses directly without declaring it in package.json, relying on another dependency to install it. If that dependency changes or removes it, your code can fail.

Q5: How does pnpm prevent phantom dependencies?

pnpm uses a non-flat node_modules layout that exposes explicitly declared packages for direct use. Undeclared packages are not available for your code to import directly.

Q6: Can a project use both NPM and pnpm?

It is not recommended. Mixing them can produce inconsistent lockfiles and cause team members to install different dependency versions.


🧪 Practice

  1. Check whether Yarn or pnpm is installed on your computer:
# Enter your command here
  1. Try installing dayjs@1.11.13 with pnpm:
# Enter your command here
  1. Inspect the project root to see which lockfile was generated.

🎉 You now know about Yarn and pnpm! This completes the NPM course. You can choose a package manager that fits your project's needs.