PNPM Package Manager: What is it and Why You Should Be Using it  - A Comprehensive Guide

sergioholgado

Sergio Holgado

Posted on July 30, 2023

PNPM Package Manager: What is it and Why You Should Be Using it  - A Comprehensive Guide

Everything about the new Fast, disk space efficient package manager that's taking over npm.

· PNPM: The New npm Challenger: Discover the rise of PNPM and why it's taking the development world by storm.

· Inside PNPM's Magic: Unravel the inner workings of PNPM and its unique approach to package management.

· Getting Started with PNPM: Step-by-step guide to effortlessly integrate PNPM into your projects with easy installation and setup.

· Mastering PNPM CLI: Learn essential CLI commands, configuration, and shortcuts to enhance your development workflow.

PNPM: The New NPM and Yarn Challenger

npm (Node Package Manager) has long been the industry standard, but a new contender has emerged: pnpm (Pinned Node Package Manager). Offering enhanced speed, resource efficiency, insightful logs and is 3 times more efficient, pnpm is positioning itself as a viable alternative to npm, showcasing why npm may be falling behind.

When comparing PNPM and Yarn, it becomes evident that PNPM outperforms Yarn in terms of speed, regardless of cache type (cold or hot). The key to PNPM's speed lies in its file linking approach, where it simply links files from the global store, while Yarn adopts a file copying method from its cache. Additionally, PNPM ensures efficient disk usage by never saving package versions more than once on a disk.

Source:pixelmatters

  • clean install: installed for the first time with no cache on the system;
  • re-install: install after the first install;
  • with cache and lockfile: remove node_modules and run install;
  • with cache: remove node_modules, lock file, and run install;
  • with lockfile: remove node_modules and cache, and run install;
  • with cache and node_modules: only remove lockfile and install gain;
  • with node_modules and lockfile: remove cache and run install again;
  • with node_modules: remove cache and lockfile, and install again.

Downsides of using PNPM

Due to its flat tree structure, pnpm does not support the lock files produced by NPM. However, there exists a convenient command that enables the conversion of NPM/Yarn lock files into a pnpm-compatible format.

It's important to note that pnpm cannot publish packages with bundledDependencies. However, it is worth mentioning that even on NPM, using bundledDependencies is not a recommended practice. Instead, the preferred approach is to employ a package bundler, such as webpack, rollup, or ESBuild, to handle the bundling process effectively. This ensures a more efficient and manageable packaging of dependencies for the project.

Inside PNPM's Magic:

Symbolic Structure

When utilizing npm, having 100 projects with the same dependency would lead to 100 separate copies of that dependency being saved on the disk. However, pnpm adopts a smarter approach by storing dependencies in a content-addressable store, resulting in the following benefits:

  1. Efficient Storage Utilization:

When depending on different versions of the same dependency, pnpm optimizes disk usage by only adding the files that differ to the store. Suppose a dependency contains 100 files, and a new version introduces changes in just one file. In that case, running "pnpm update" will only add the modified file to the store instead of cloning the entire dependency, substantially reducing storage overhead.

  1. Centralized File Storage:

Instead of using the flatten tree structure PNPM uses a symbolic link structure, this approach results in all dependency files being consolidated into a single location on the disk. During the installation process, pnpm creates hard links from this central repository to each project, eliminating the need for redundant copies. This innovative approach allows different projects to share dependencies of the same version, saving considerable disk space.

The cumulative result of these optimizations is a significant reduction in disk space consumption, directly proportional to the number of projects and their associated dependencies. Additionally, pnpm achieves faster installations due to its streamlined approach, making it a highly efficient package manager choice for developers.

Getting Started with PNPM:

Installation

Installing with Windows Powershell:

iwr https://get.pnpm.io/install.ps1 -useb | iex

Installing with NPM
· Node.js already installed:
npm install -g pnpm
· Node.js not installed (included in package as exe)
npm install -g @pnpm/exe

Install with Chocolatey
choco install pnpm

  • pnpm install

The pnpm install command is used to install project dependencies. It creates a non-flat node_modules structure in a content-addressable store, optimizing storage and providing faster installations. It also supports multi-package installations in a repository.

pnpm install

  • pnpm add

The pnpm add command allows you to add new dependencies to your project. It automatically updates the package.json file and installs the new package.

pnpm add

  • pnpm update

The pnpm update command helps update dependencies to their latest versions. It efficiently updates only the changed files in the content-addressable store, reducing the disk space required.

pnpm update

  • pnpm remove

To remove dependencies from the project, you can use the pnpm remove command. It removes the specified packages and updates the package.json file accordingly.

pnpm remove

· pnpm list

The pnpm list command displays a tree-like view of the project's dependencies. It shows all installed packages and their respective versions.

pnpm list

· pnpm store status

The pnpm store status command provides an overview of the content-addressable store's status, displaying its size and the number of packages stored.

pnpm store status

· pnpm store prune

To optimize disk space usage, you can use the pnpm store prune command. It removes unreferenced packages from the store.

pnmp store prune

· pnpm publish

The pnpm publish command enables developers to publish their packages to registries and share them with the community. When you publish a package using PNPM, it employs the content-addressable store to optimize the publishing process.
Instead of duplicating package files for each version, PNPM links common files from the store, drastically reducing the size of the published package. This intelligent approach leads to faster publishing times and more efficient storage usage on the registry, contributing to a better overall developer experience.

pnpm publish

PNPM Workspaces

PNPM's Workspaces feature allows developers to manage multiple packages within a monorepository with remarkable ease. By defining a single package.json file at the root of the repository and specifying the individual packages as workspaces, PNPM treats them as interdependent projects.
When you run pnpm installat the root of the repository, PNPM installs dependencies for all workspaces, ensuring consistency across the entire project. This centralized approach streamlines development, facilitates code sharing, and simplifies the maintenance of complex projects.



{
 "name": "my-monorepo",
 "private": true,
 "workspaces": [
 "packages/*"
 ]
}


Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
sergioholgado
Sergio Holgado

Posted on July 30, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related