# pnpm

> The pnpm (performant npm) is an alternative to npm that focuses on performance and efficient disk space usage by using a single store for all packages.

## What is pnpm and how is it different from npm?

The package manager that offers faster installations and saves disk space by using a content-addressable store for packages. It's compatible with npm but optimizes package handling.

- Topic: Package Managers
- Type: package manager, tool

## Example

Wrong:

```bash
npm install -g pnpm
npm install zod
```

Right:

```bash
corepack enable
pnpm add zod
```

Corepack provides the pnpm version pinned in `package.json`, and `pnpm add` keeps `pnpm-lock.yaml` as the only lock file.

## Common mistakes

- Imports break after switching to pnpm because the code used packages it never declared.
- Running `npm install` in a pnpm project and creating a second lock file.

## Key points

- Commands use `pnpm add` and `pnpm install`, not `npm install`, and only `pnpm-lock.yaml` is committed.
- CI installs with `pnpm install --frozen-lockfile`.
- Every package the code imports is listed in its own `package.json`, since pnpm blocks undeclared dependencies.
- Internal monorepo packages are referenced with the `workspace:` protocol.

## Interview questions

### Why does pnpm save disk space?

pnpm keeps one copy of each package version in a global store and links it into every project's `node_modules`, instead of copying it each time.

### Why can't my code import a package after switching to pnpm?

pnpm only exposes packages declared in your `package.json`. npm's flat `node_modules` let you import transitive dependencies by accident. Add the missing package explicitly.

### How do I run a package once with pnpm?

Use `pnpm dlx <package>`, the pnpm version of `npx`.

## Related cards

- [Package Managers](https://wonderium.dev/processes/package-managers.md)
- [npm](https://wonderium.dev/processes/npm.md)
- [Yarn](https://wonderium.dev/processes/yarn.md)
- [Workspaces](https://wonderium.dev/processes/workspaces.md)
- [Lock file](https://wonderium.dev/processes/lock-file.md)

---

- Level: [Processes](https://wonderium.dev/processes.md)
- HTML version: https://wonderium.dev/processes/pnpm
- Last updated: 2026-11-25
