Software
March 1, 2023

PNPM vs NPM - A case study at Yahoo!

PNPM as a faster alternative to NPM

Introduction

PNPM and NPM are both package managers for JavaScript and Node.js, but they have some key differences. In this article, we will take a closer look at both PNPM and NPM, compare their features, and help you decide which one is the best fit for your project.

What is PNPM?

PNPM (Performance npm) is a relatively new package manager that was created with the goal of improving performance over npm. It achieves this by using hard links and symlinks to create a single, shared copy of each package. This means that instead of each package being stored multiple times in different node_modules directories, there is only one copy that is shared across all projects. This reduces the amount of disk space used, and also speeds up installation times, as packages don't need to be re-downloaded and re-installed.

One of the most significant benefits of PNPM is the speed of installation. According to the official website, PNPM can be up to 4 times faster than npm, and up to 2 times faster than Yarn. This can be particularly beneficial for large projects with many dependencies.

On few established projects at Yahoo! installation speed during CI/CD jobs dropped from minutes to a few second. Obviously, to take advantage of this you have to configure caching in your continuous deployment tool. You can find more details for how to do that here.

Comparison in install speed for a new React project using NPM and PNPM

Monorepos?

Another advantage of PNPM is its ability to handle large monorepos. A monorepo is a single repository that contains multiple projects, as opposed to a traditional repository that contains only one project. PNPM can handle large monorepos with ease, as it only installs the packages that are needed for each project, instead of installing all packages for all projects. This can save a lot of disk space and make the installation process much faster.

We gave it a try at Yahoo! but found that its a very manual process. Sure, PNPM made it easier to install and configure dependencies in a Monorepo setting, but we still had to manually configure the build, linting and the dependency between the monorepo packages themselves. I would recommend choosing a tool more geared towards monorepos like Lerna or NX.

PNPM also has a feature called "store sharing" which allows to share store between different projects. This means that when you install a package in one project, it will be shared with other projects that use the same package. This can significantly reduce the amount of disk space used.

On the other hand, npm (Node Package Manager) is the most popular package manager for JavaScript and Node.js, and has been around for much longer than PNPM. It has a large and active community, and there are a lot of resources available for learning and troubleshooting.

Conclusion

In conclusion, both PNPM and NPM are great package managers, and they have their own strengths and weaknesses. PNPM is a newer package manager that is focused on performance and handling large monorepos, while npm is a more established package manager with a wider range of packages and built-in security features. If you're working on a large project with many dependencies and are looking for a faster and more efficient package manager, then PNPM may be the better choice. On the other hand, if you want a more established package manager with a wide range of packages and built-in security features, then npm is the way to go. Ultimately, the choice between PNPM and NPM will depend on your specific needs and the type of project you're working on.