Yarn and pnpm: Alternative package managers
🎯 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_moduleskeeps its own copies of packages, duplicating many files. - Inconsistent dependency trees: running
npm installat 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.locklockfile: 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 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:
{
"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:
import { createApp } from 'vue'; // ⚠️ vue is not declared in package.json!
Here is the problem:
- Your
package.jsondoes not declarevue, but your code relies on it. - If an
element-plusupdate removes its dependency onvueor changes its version, your code can fail. - 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.
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 codepackages/utils: utility functionsapps/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:
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.
⚖️ Comparing NPM, Yarn, and pnpm
| Feature | NPM | Yarn | pnpm |
|---|---|---|---|
| Maintainer / origin | Bundled with Node.js | Facebook (now Meta) | Open-source community |
| Installation speed | Fairly fast (significantly improved since v5) | Fast | Very fast |
| Disk usage | High | High | Low (content-addressable storage) |
| Lockfile | package-lock.json | yarn.lock | pnpm-lock.yaml |
| Dependency layout | Flat | Flat | Non-flat and stricter |
| Phantom dependencies | Can occur easily | Can occur easily | Largely prevented |
| Workspaces | Supported | Supported | Native support with efficient storage |
| Typical use cases | General use; bundled with Node.js | Large projects and monorepos | Fast installations, strict dependency management, and monorepos |
💡 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.
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
- Check whether Yarn or pnpm is installed on your computer:
# Enter your command here
- Try installing
dayjs@1.11.13with pnpm:
# Enter your command here
- 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.
