Why You Should Use pnpm for Your Next Project

Why You Should Use pnpm for Your Next Project

As JavaScript projects grow in complexity, so do their dependency trees. If you’ve been using npm or Yarn, you might have noticed (or haven’t) your node_modules folder becoming increasingly bloated. Enter pnpm: a package manager that’s gaining popularity for good reason. Let’s explore why you should consider switching to pnpm for your next project.

Dramatically Reduces Disk Space Usage

Unlike npm and Yarn, which create a copy of each dependency for every project, pnpm uses a content-addressable store.

In plain English:

  1. All packages are stored in a global store on your disk
  2. Files are linked from the store to your project’s node_modules
  3. Each unique version of a package is stored only once on your entire system

This can save gigabytes of disk space!

Enforces Proper Dependency Management

“It works on my machine” is all too common in npm land. It might be because your code unknowingly used a transitive dependency. pnpm solves this with its strict node_modules structure.

It creates a symlink structure that only exposes the packages you’ve explicitly declared as dependencies. This means your code can only access the dependencies you’ve properly listed in your package.json. No more silent reliance on nested packages that happen to be installed but aren’t declared in your project. Your projects are now more maintainable and less prone to mysterious bugs!

Lightning-Fast Performance

Speed matters when you’re installing dependencies multiple times a day. pnpm consistently outperforms both npm and Yarn in benchmarks for installation speed.

The performance gains come from:

  • Parallel operations that efficiently utilize your CPU
  • Minimal disk I/O due to its linking strategy
  • Optimized algorithm for dependency resolution

The pnpm dlx Command: A Better npx

Just like npx lets you execute packages without installing them globally, pnpm offers the dlx command with similar functionality but with all the benefits of pnpm’s architecture.

For example, instead of:

npx cypress open

You can use:

pnpm dlx cypress open

The dlx command ensures that packages are executed in an isolated environment, preventing pollution of your global space.

Save Your Fingers with Aliases

If you get spaghetti fingers from typing “pnpm”, you can easily create a shell alias to save keystrokes. This is actually a common pain point that many developers experience.

In fact, I commented on this very issue back in 2022:

"This might be an old thread but I'd say an alias would be appreciated. I used pnpm for the first time the other day from a tutorial and I had so much trouble consistently typing 'pnpm'. It must be a cognitive process judging from others' responses here."

Which lead to this easy fix you can add to your .bashrc, .zshrc, or equivalent shell configuration file:

alias pn=pnpm

After reloading your shell, you can simply type:

pn create astro@latest
pn run dev

Much easier on the fingers!

How Does It Work Under the Hood?

Pnpm creates a unique node_modules structure that uses symbolic links to create the illusion of a traditional node_modules folder while maintaining its space-efficient approach.

When you install a package, pnpm:

  1. Places the package in a global content-addressable store (typically in ~/.pnpm-store)
  2. Creates a hard link from the store to your project’s .pnpm directory
  3. Creates symbolic links from the .pnpm directory to your project’s node_modules

This structure ensures that Node.js’s module resolution algorithm works correctly while preventing access to undeclared dependencies.

Making the Switch

Ready to give pnpm a try? Installation is straightforward:

# Update corepack first
npm install --global corepack@latest

# Enable pnpm
corepack enable pnpm

Converting an existing project is as simple as removing your node_modules folder and running pnpm install. You should also remove your package-lock.json file if it exists.

For most projects, the transition is seamless, and you’ll immediately start benefiting from faster installations and reduced disk usage.

Give it a try on your next project—your disk space and your future self will thank you!