The Most Underrated Package Manager

The Advantages of PNPM over NPM and Yarn

The Most Underrated Package Manager

As a beginner, starting my developer career, I understand the importance of choosing the right tools to enhance development efficiency. One crucial aspect of the development process is managing software packages and their dependencies. That’s where package managers like NPM, PNPM, and Yarn come into play. In this article, I will explore the advantages of PNPM over NPM and Yarn, and why it is gaining popularity among companies and developers worldwide.

The Issues with NPM and Yarn

Before diving into the benefits of PNPM, let’s first understand the challenges posed by traditional package managers like NPM and Yarn. NPM, widely used as the standard package manager for JavaScript, has a few inherent issues. One of them is the flattened **node_modules** structure. This flattening of the dependency tree leads to complexities in managing dependencies, duplicating packages, and modules having access to packages they do not explicitly depend on.

To illustrate this issue, let’s consider a basic example. Imagine you have a project that requires the “express” module, which, in turn, has a dependency named “debug.” In the NPM structure, both “express” and “debug” will be placed directly under the root-level node_modules directory. This means that even if your code does not explicitly depend on the "debug" package, it can still be accessed, causing potential issues when updating or removing dependencies.

Yarn, another popular package manager, improves on some of the disk space optimization issues of NPM by utilizing hoisting. However, this approach also has limitations and may fail in some instances. The need for a more efficient and reliable package manager has led to the emergence of PNPM.

Introducing PNPM

PNPM is a drop-in replacement for NPM, built on top of NPM itself. It offers significant advantages over its predecessors, making it a perfect choice for enhancing development efficiency. One of the key features of PNPM is its highly disk-efficient approach, which solves many of the issues faced by NPM and Yarn.

Unlike NPM and Yarn, which use flattened node_modules structures, PNPM utilizes hard links and symlinks to maintain a semistrict node_modules structure. A hard link is a different reference to the same file, while a symlink is a symbolic link that points to another path. By creating a nested structure of dependencies using symlinks, PNPM avoids the problems of duplicated packages and implicit access to unnecessary dependencies.

Let’s take a closer look at the directory structure of a project using PNPM. Instead of placing all modules directly under the root level node_modules directory, PNPM creates a special folder named ".pnpm" that contains hard links to all the modules. This structure ensures that modules can only access their specific dependencies and prevents unnecessary duplication.

Additionally, PNPM introduces a global store, known as the “pnpm-store,” where all the downloaded dependencies are maintained. When a dependency is downloaded, PNPM checks if it already exists in the store and creates a hard link to it if it does. This approach enables PNPM to reuse the same packages across multiple projects, significantly reducing disk space usage.

By reusing packages and utilizing hard links, PNPM optimizes disk space and reduces the time and effort required for package installation and updates. These advantages make it a valuable tool for developers, especially those working on large mono repo projects.

The Advantages of PNPM

Now that we understand the underlying principles of PNPM, let’s explore its advantages in more detail. PNPM offers several key benefits over NPM and Yarn, making it a compelling choice for developers and companies.

1. Disk Space Efficiency

One of the standout features of PNPM is its disk space efficiency. Unlike NPM and Yarn, which can lead to duplicated packages and bloated node_modules directories, PNPM uses a content-addressable file system to store packages and dependencies. This means that the same package will not be duplicated, even with different versions. PNPM intelligently reuses the maximum amount of code, reducing disk space consumption significantly.

For example, if a package has 500 files in version 1 and just one additional file in version 2, PNPM will create a hard link to the original 500 files and only write the new file for version 2. In comparison, NPM would duplicate all 500 files for version 2, resulting in unnecessary disk space usage. This efficiency is particularly beneficial for large mono-repo projects that rely on shared packages across multiple applications.

2. Faster Package Installation

Another major advantage of PNPM is its speed. PNPM outperforms NPM and Yarn in terms of package installation time. Benchmark tests have consistently shown that PNPM performs better in most cases, allowing developers to save valuable time and improve overall development efficiency.

The faster package installation speed of PNPM can be attributed to its optimized file system structure and the use of hard links. By avoiding unnecessary duplication and reusing packages already present in the global store, PNPM eliminates the need for time-consuming downloads and installations. This advantage becomes even more significant as projects scale and the number of dependencies increases.

3. Enhanced Dependency Management

PNPM offers improved dependency management compared to NPM and Yarn. With NPM and Yarn, dependencies can be accessed implicitly, even if they are not explicitly declared in the project’s package.json file. This can lead to version conflicts, compatibility issues, and unintended access to unnecessary packages.

In contrast, PNPM uses a different dependency resolution algorithm and folder structure for node_modules, ensuring that packages can only access their specific dependencies. This strict control over dependencies reduces the likelihood of conflicts and improves the overall stability and robustness of the development environment. This becomes particularly important in large mono-repo setups, where managing dependencies across multiple projects can be challenging.

4. Integrity Checks

PNPM, like Yarn, implements integrity checks to ensure the integrity of all installed packages before executing their code. This verification process helps eliminate potential security risks and ensures that the codebase remains secure and reliable. By verifying the checksum of installed packages, PNPM provides an additional layer of protection against tampered or compromised dependencies.

5. Monorepo and Offline Mode Support

PNPM offers excellent support for mono-repo projects, making it an ideal choice for organizations working on multiple interconnected applications. With PNPM, managing dependencies across different projects within a mono-repo becomes more streamlined and efficient. The ability to share packages and avoid duplication further enhances productivity and reduces development time.

Additionally, PNPM provides robust offline mode support. Developers can continue working on their projects even without an internet connection, as PNPM caches all the necessary dependencies in the global store. This feature is especially useful for developers working remotely or in low-connectivity environments.

PNPM CLI and Migration

PNPM comes with a user-friendly command-line interface (CLI) that facilitates seamless integration into existing development workflows. The PNPM CLI offers a set of commands that simplify package management tasks.

Here are some of the basic commands available in the PNPM CLI:

  • pnpm init: Creates a new package.json file.

  • pnpm install: Downloads and installs all the packages listed as dependencies in the package.json file.

  • pnpm add [module_name]@[version]: Downloads a specific version of a package and adds it to the list of dependencies in package.json.

  • pnpm remove [module_name]: Uninstalls and removes a package from the list of dependencies in package.json.

  • pnpm list: Displays a tree of locally installed modules.

Migrating from NPM or Yarn to PNPM is relatively straightforward. PNPM is designed as a drop-in replacement for NPM, meaning that existing NPM projects can transition to PNPM without significant modifications to the codebase. The PNPM CLI offers commands that are similar to those used in NPM and Yarn, making the migration process smoother.

End of NPM and YARN, Start of your Productivity

As a junior developer, I understand the importance of optimizing development efficiency and streamlining the package management process. PNPM offers a range of advantages over traditional package managers like NPM and Yarn. Its disk space efficiency, faster package installation speed, enhanced dependency management, integrity checks, and support for mono-repo and offline modes make it an excellent choice for developers and organizations.

By adopting PNPM, developers can reduce disk space usage, improve package installation time, and ensure the stability and security of their codebase. The user-friendly PNPM CLI simplifies package management tasks, while the seamless migration process from NPM and Yarn ensures a smooth transition.

As PNPM gains popularity among giant tech companies like Vue3, Prism, and Microsoft, more developers are recognizing its potential to boost development efficiency and streamline workflows. Embracing PNPM as a junior developer can be a significant step towards enhancing productivity and delivering high-quality software projects.

This was a learning for me from a recent internet surf, The knowledge was shared by Mr. Muhammad Khabbab. Special Thanks to him.

Did you find this article valuable?

Support Komal Vardhan Lolugu by becoming a sponsor. Any amount is appreciated!