What is pnpm? The fast and efficient nodejs package manager.

Photo of author
Written By Tristan

pnpm is a super-fast and disk space efficient alternative to npm and Yarn for Node.js. Learn how pnpm works and why it’s better for modern JavaScript projects.

Introduction

JavaScript developers today have access to the largest collection of open source packages in history via npm, the default package manager for Node.js. However, as JavaScript projects grow in complexity, managing dependencies and dealing with disk space bloat is becoming a pain point. This is where pnpm comes in – a fast, disk space efficient alternative package manager designed specifically for modern JavaScript applications.

pnpm (pronounced: p-npm) offers game-changing improvements in speed, disk usage, and security over both npm and Yarn. While relatively new, it has quickly become a favorite among JavaScript developers working on large projects.

In this post, we will understand what pnpm is, how it works, its key benefits, and why JavaScript developers should consider switching to pnpm as their package manager of choice.

What is pnpm?

pnpm stands for “performant npm”. It is a Node.js package manager alternative to npm and Yarn that installs project dependencies using a content-addressable file system.

Instead of copying packages into node_modules like npm does, pnpm symlinks packages from a global store on your disk. This avoids duplicating files across projects, saving huge amounts of disk space.

pnpm was created in 2017 by Zoltan Kochan, motivated by the disk space and speed problems of npm install. It has since become one of the fastest growing and most efficient package managers for JavaScript.

Key Features and Benefits

Here are some of the standout features and benefits of using pnpm over npm or Yarn:

  • Fast installation: pnpm installs dependencies in parallel using hard links. This makes installation up to 3x faster.
  • Lower disk usage: By symlinking from a global store instead of copying, pnpm saves over 90% in disk space.
  • Built-in dependency optimization: pnpm dedupes and hoists common dependencies for faster installs.
  • Strictness: pnpm uses checksums and a content-addressed file system to avoid dependency conflicts.
  • Scales to large projects: pnpm remains performant and efficient even for projects with hundreds of dependencies.
  • Backward compatibility: pnpm ensures compatibility with npm registries and the wider Node.js ecosystem.

How pnpm Works

pnpm manages dependencies in a fundamentally different way from npm and Yarn under the hood. Here’s a quick look at how it works:

  • Packages are stored in a single, global store folder (default: ~/.pnpm-store).
  • Instead of copying packages into node_modules, pnpm creates symlinks from the store.
  • The store uses content-addressing – packages are saved by content hash, avoiding duplication.
  • A shrinkwrap file locks down the dependency tree. Adding a new package creates a new lockfile.
  • Transitive dependency hoisting is used to flatten module locations.

This optimized install strategy allows super fast installs and reduced disk usage. The store persists across projects to maximize efficiency.

Why Use pnpm?

For modern, complex JavaScript applications with hundreds of direct and indirect dependencies, pnpm is a no-brainer over npm.

Here are some of the top reasons to switch to pnpm:

  • blazing fast installs – you get your dependencies 3x faster
  • save over 90% in disk space for your projects
  • avoid node_modules duplications across projects
  • strictness and reproducibility with content-addressing
  • efficient dependency optimization under the hood
  • excellent scaling even for massive projects
  • easy migration from npm

For large JavaScript codebases and monorepos, pnpm is enabling faster CI builds, simplified deployment, and easier local development. The performance and disk benefits are especially noticeable compared to npm.

Getting Started

To start using pnpm, first install it globally:

npm install -g pnpm

Then install dependencies in your project:

pnpm install

That’s it! pnpm will now manage your packages from its global store.

To learn more about migrating from npm or Yarn and using pnpm effectively, check out the excellent pnpm docs.

Conclusion

pnpm is a promising new package manager built specifically for the challenges of modern JavaScript dependency management.

With its content-addressable store, symlinking strategy, and built-in optimization, pnpm unlocks game-changing benefits in install speed, disk usage, and strictness.

As JavaScript projects grow larger and more complex, pnpm is the most efficient package manager available today. For any serious JavaScript developer working on a complex codebase, it’s worth taking pnpm for a spin.