What is pnpm and why you should use it?
30 August, 2023
0
0
0
Contributors
Managing dependencies is one of the most essential aspects of developing any software solution. NPM and Yarn have been the go-to package managers for Node.js developers for quite some time. However, these package managers have their own limitations, such as slower installation times, excessive disk usage, and storage issues. In this blog, we will discuss pnpm, a new and innovative package manager that efficiently addresses these challenges.
What is PNPM?
In simple terms, PNPM (Performant NPM) is a forward-thinking package management solution designed to address the challenges posed by traditional package managers. At its core, PNPM employs a centralized storage system combined with hard links to streamline the way dependencies are managed in JavaScript projects. Unlike NPM and Yarn, which tend to duplicate packages for each project, PNPM utilizes a content-addressable store to create hard links to packages from the virtual store, drastically reducing redundancy and disk space consumption.
How Does PNPM Differ from NPM?
PNPM introduces a unique approach to package management that sets it apart from its predecessors. Let's explore how PNPM differs from NPM:
Dependency Management:
- PNPM: Utilizes a shared dependency mechanism that allows different projects to use the same copy of a package. This efficient approach minimizes duplication and reduces disk space usage.
- NPM: Employs a flat dependency architecture where each project has its own copy of all dependencies, potentially leading to higher disk space consumption.
Disk Space Usage:
- PNPM: Requires less disk space due to its shared dependency model. Packages are stored centrally and linked to projects, eliminating the need for redundant copies.
- NPM: Consumes more disk space as each project maintains separate copies of dependencies.
Installation Speed:
- PNPM: Installation times are expedited by the shared dependency approach. Packages are installed only once, leading to faster subsequent installations, especially for commonly used packages.
- NPM: Installation times might be longer due to its distinct copy mechanism for each project's dependencies.
Workflow Integration:
- PNPM: While PNPM supports well-known workflows, some tools or frameworks that heavily rely on NPM might not work seamlessly with it.
- NPM: Enjoys a vast array of tooling integrations and support built around it, thanks to its established community and extensive package registry.
Community and Ecosystem:
- PNPM: As a newer package manager, PNPM has a smaller community and package registry compared to NPM.
- NPM: Boasts a larger and well-established community, making it easy to find and utilize a wide range of packages.
Features of PNPM
PNPM comes equipped with several features that set it apart from traditional package managers:
Workspace Support
PNPM's "workspaces" feature enables establishing dependencies between packages within a mono repo. This feature simplifies managing interconnected projects under a single repository.
Aliases
PNPM introduces the concept of "aliases," allowing you to define custom shortcuts or alternate names for packages and modules. This simplifies module imports and enhances the accessibility of dependencies, particularly in complex project structures.
Command Line Tab-Completion
PNPM stands out by offering command-line tab completion for various shells like Bash, Zsh, and Fish. This enhances the developer experience and sets it apart from other package managers who often require plugins for similar functionality.
Why Consider Using PNPM?
Now that we understand the core differences between PNPM and its counterparts, let's explore the compelling reasons why you should consider adopting PNPM for your projects:
Efficient Disk Space Utilization:
PNPM's shared storage system and hard links mean packages are stored only once on the disk, regardless of how many projects use them. This significantly reduces disk space consumption, making it ideal for projects with multiple dependencies or limited storage.
Faster Package Installations and Updates
Hard links and shared packages in PNPM lead to faster package installations and updates. Since most package files are already present, redundant copying or downloading is minimized, improving your development workflow.
Resource Optimization:
PNPM's approach optimizes both CPU and memory usage, making it suitable for small projects and resource-constrained environments. This can result in smoother development processes and quicker build times.
Suitable for Various Use Cases:
PNPM is well-suited for monorepos with interconnected packages, microservices-based projects, and projects with limited disk space. Its shared storage approach streamlines dependency management in complex scenarios.
How to install pnpm?
Installing pnpm on your local machine can be as hard or as simple as you want. You can install it as a standalone script without even requiring Node.js installed. But we are not going that path as we most likely have Node.js installed on our machine.
It is easier to install pnpm with Node.js and NPM preinstalled. There are two CLI packages for pnpm: pnpm
and @pnpm/exe
.
pnpm
is an ordinary version of pnpm, which needs Node.js to run.
npm install -g pnpm
@pnpm/exe
is packaged with Node.js into an executable, so it may be used on a system with no Node.js installed.
npm install -g @pnpm/exe
Once installed, you can confirm the installation process by executing the following command. This command gives the current version of pnpm installed on your machine.
pnpm -v
Now that we have installed pnpm on our machine, let's explore some basic commands you will use on a daily basis.
Basic pnpm commands
pnpm add <package_name>
: Installs the package and other packages that it depends on. By default, new packages are installed as a production dependency. You can install it as a dev dependency with-D
flag.pnpm install
orpnpm i
: Installs project dependencies in your Node.js projectpnpm remove <package_name>
: Uninstalls dependencies from project'snode_modules
and removes them frompackage.json
. Has three aliases:rm
,uninstall
, andun
.pnpm update <package_name>
: This command updates the package to its latest version based on the specified range. If we don't provide any argument, it updates all dependencies. Has two aliases:up
andupgrade
.
Closing notes
If you are working with multiple projects on your machine, or don't have much storage space, you should consider pnpm over NPM or Yarn. pnpm is a faster, more efficient, and strict package manager that saves disk space required to store node dependencies.
npm
pnpm
yarn
alternative