pnpm – a Fast, Disk Space Efficient Package Manager for JavaScript

Rate this content
Bookmark

You will learn about one of the most popular package managers for JavaScript and its advantages over npm and Yarn.

  • A brief history of JavaScript package managers
  • The isolated node_modules structure created pnpm
  • What makes pnpm so fast
  • What makes pnpm disk space efficient
  • Monorepo support
  • Managing Node.js versions with pnpm

FAQ

Pnpm is a fast and disk space efficient package manager for JavaScript that aims to address issues found in npm versions 3 and 4. It utilizes a unique node module structure to ensure that dependencies are isolated and only direct dependencies are accessible to a project, reducing the risk of breaking changes.

Pnpm saves disk space by using a content addressable storage system, wherein files are saved based on a hash code derived from their content. This approach means if multiple projects use the same package, they will share the same files in the storage, significantly reducing disk space usage.

Pnpm is faster because it handles package installation stages separately for each dependency. This allows some packages to be resolved or fetched while others are still being fetched or written to node modules, enhancing the speed of operations.

Yes, pnpm provides excellent support for monorepos. It maintains an isolated module structure, ensuring that projects within a monorepo only have access to their own dependencies, which prevents accidental access to unrelated project dependencies.

Pnpm uses both symlinks and hardlinks to manage files. Symlinks are used to reference the real location of packages, aiding Node.js in dependency resolution. Hardlinks are used to link files from the content addressable store, saving disk space without creating duplicate files.

Migrating from YARN to pnpm can be straightforward, especially for single package repositories. Pnpm provides an 'import' command that converts a YARN lock file to a pnpm lock file, making the transition smoother. For workspaces, adjustments might be needed for package listing due to differences in configuration files between YARN and pnpm.

Pnpm can handle multiple drives or filesystems by creating separate storage for each disk. This approach maintains efficiency on a single disk using hardlinks, but will resort to copying files across different disks, slightly reducing the space efficiency benefits in such scenarios.

Zoltan Kochan
Zoltan Kochan
31 min
24 Mar, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

pnpm is a fast and efficient package manager that gained popularity in 2021 and is used by big tech companies like Microsoft and TikTok. It has a unique isolated node module structure that prevents package conflicts and ensures each project only has access to its own dependencies. pnpm also offers superior monorepo support with its node module structure. It solves the disk space usage issue by using a content addressable storage, reducing disk space consumption. pnpm is incredibly fast due to its installation process and deterministic node module structure. It also allows file linking using hardlinks instead of symlinks.

1. Introduction to pnpm and its Popularity

Short description:

Hello, my name is Zoltan Kochan and today I'm going to talk about pnpm, which is a fast disk space efficient package manager. Let me introduce myself. I work remotely for BIT, a company that helps developers implement component-based development. Before that, I worked for Just Answer for nine years and have been developing and maintaining npm-pm since 2016. npm, yarn, and pnpm are the most popular JavaScript package managers. npm had issues in the past, but alternatives like yarn and pnpm were created. pnpm is an indie project that fixes the issues of npm v3 and is now supported by bit. It had a spike in popularity in 2021 and is used by big tech companies like Microsoft and TikTok. pnpm is unique because it only hoists the necessary dependencies to the root of node modules.

Hello, my name is Zoltan Kochan and today I'm going to talk about pnpm, which is a fast disk space efficient package manager.

Before that let me introduce myself. I'm Zoltan Kochan. I was born and raised in Ukraine. I currently live in Ukraine as well. For now I'm safe. I work remotely for BIT. BIT is a company that helps developers to implement component-based development. Before BIT I worked for Just Answer for nine years and in the meanwhile, since 2016, I'm constantly developing and maintaining npm-pm.

Before talking about npm-pm let's briefly talk about other Javascript package managers. The most popular node.js or javascript package manager is npm which is the official package manager of the npm registry. It is shipped by node.js and in the past npm had a lot of issues, like it was slow, it was undeterministic, it was giving funny results sometimes, so some alternatives were created. One of the alternatives was created by facebook, you probably heard about yarn, which is the second most popular package manager after npm. It is now maintained by the community, and it did solve a lot of the issues that npm had in version 3 and 4. Since then, in v2, yarn has switched to use plug and play by default, so even though it does support classic node modules installation, now it prefers to use the plug and play, which many love and many hate. I personally think it's a cool feature. And yarn is currently shipped with the latest version of Node.js. The 3rd most popular package manager is pnpm. It is a completely indie project, it was created by open source contributors to fix the issues of npm v3. At the same time as yarn 2, so pnpm is not like a new project, it exists since yarn exists, basically. And now it is supported by bit, because I work at bit and pnpm is heavily used in the bitcli for package management and pnpm as well is shipped with nodjs through the corepack feature of nodjs. If we compare these package managers by popularity, obviously npm is currently the most popular one, then yarn and pnpm is the less popular, even though pnpm came out at the same time as yarn, but of course facebook had a lot of marketing power to make yarn very popular at the start. However, even though pnpm is less popular for now, it had a big spike in popularity last year, so in 2021 pnpm was downloaded three times more than in 2020. We have a lot of big tech companies that already use pnpm, so even Microsoft uses pnpm in some of its projects, and the TikTok frontend team uses pnpm. So pnpm works really well, and is production ready for sure.

Let's see what makes pnpm unique. When you install a dependency with npm or Yarn Classic, all the sub-dependencies are hoisted to the root of node modules. As you may see in this example, even though only Express is in the dependencies, all those other packages are also hoisted to the root of node modules. On the contrary, pnpm is only put in Express to the root of node modules. So even though cookie is not a dependency of your project, importing it will work. This is a dangerous situation.

2. Isolated Node Module Structure with PMPM

Short description:

But what will happen if a new version of Express comes out and cookie is not in its dependencies anymore? Your package will break. PMPM prevents such mistakes. With PMPM your code has only access to packages that are declared as dependencies of your project. In this example, Express is a direct dependency and KUKI is a dependency of Express. The real location of Express is inside the .pmpm folder. So nodejs will search for KUKI in the dedicated nodemodules folder which is at .pmpm express at 4.17.3/.nodemodules. Each package is in its own isolated folder with its own dependencies.

But what will happen if a new version of Express comes out and cookie is not in its dependencies anymore? Your package will break. PMPM prevents such mistakes.

With PMPM your code has only access to packages that are declared as dependencies of your project. How is PMPM able to create such an isolated node module structure?

In this example you see Express is a direct dependency and KUKI is a dependency of Express. As you can see, only Express is placed to the root of the project's node modules folder. And KUKI and actually that Express inside node modules, it is a seamlink, it is just a seamlink to a folder inside the .pmpm folder.

This works because when searching for dependencies the node.js resolution algorithm is using the real location of the package as a starting point for searching its dependencies. The real location of Express is inside the .pmpm folder. So nodejs will search for KUKI in the dedicated nodemodules folder which is at .pmpm express at 4.17.3/.nodemodules. And of course you can see that KUKI inside the nodemodules of Express is also just a symlink to a folder with the real location of KUKI.

So let me show you a quick demo. I have an empty project. I run npm add express. Installation is completed. Let's see what we got here. Ignore this. This is something we hoist by default to fix some issues. But you can see that Express is only hoisted and it is a Syn-Link. The real location of Express is here. And here are in this folder also all the other direct dependencies of Express.

So you can see here cookie. But cookie is actually a symlink to a dedicated isolated folder for the cookie device. You can see here. Actually, cookie is here. And this is a symlink. This is also a symlink to this folder of the accept package. So this is a real location of accept. And these are dependencies of accept. So you can see that each package is in its own isolated folder with its own dependencies. Let's return to the slides.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Modern Web Debugging
JSNation 2023JSNation 2023
29 min
Modern Web Debugging
Top Content
Few developers enjoy debugging, and debugging can be complex for modern web apps because of the multiple frameworks, languages, and libraries used. But, developer tools have come a long way in making the process easier. In this talk, Jecelyn will dig into the modern state of debugging, improvements in DevTools, and how you can use them to reliably debug your apps.
The Future of Performance Tooling
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Top Content
Our understanding of performance & user-experience has heavily evolved over the years. Web Developer Tooling needs to similarly evolve to make sure it is user-centric, actionable and contextual where modern experiences are concerned. In this talk, Addy will walk you through Chrome and others have been thinking about this problem and what updates they've been making to performance tools to lower the friction for building great experiences on the web.
Yarn 4 - Modern Package Management
JSNation 2022JSNation 2022
28 min
Yarn 4 - Modern Package Management
Top Content
Yarn 4 is the next major release of your favourite JavaScript package manager, with a focus on performance, security, and developer experience. All through this talk we'll go over its new features, major changes, and share our long-term plans for the project.If you only heard about Yarn without trying it yet, if you're not sure why people make such a fuss over package managers, if you wonder how your package manager can make your work simpler and safer, this is the perfect talk for you!
Beyond Virtual Lists: How to Render 100K Items with 100s of Updates/sec in React
React Advanced Conference 2021React Advanced Conference 2021
27 min
Beyond Virtual Lists: How to Render 100K Items with 100s of Updates/sec in React
Top Content
There is generally a good understanding on how to render large (say, 100K items) datasets using virtual lists, …if they remain largely static. But what if new entries are being added or updated at a rate of hundreds per second? And what if the user should be able to filter and sort them freely? How can we stay responsive in such scenarios? In this talk we discuss how Flipper introduced map-reduce inspired FSRW transformations to handle such scenarios gracefully. By applying the techniques introduced in this talk Flipper frame rates increased at least 10-fold and we hope to open-source this approach soon.
High-Speed Web Applications: Beyond the Basics
JSNation 2022JSNation 2022
30 min
High-Speed Web Applications: Beyond the Basics
Knowing how to run performance tests on your web application properly is one thing, and putting those metrics to good use is another. And both these aspects are crucial to the overall success of your performance optimization efforts. However, it can be quite an endeavor at times for it means you need to have a precise understanding of all the ins and outs of both performance data and performance tooling. This talk will shed light on how to overcome this challenge and walk you through the pitfalls and tricks of the trade of Chrome DevTools, providing you with a complete roadmap for performance analysis and optimization.
Debugging with Chrome DevTools
JSNation Live 2021JSNation Live 2021
11 min
Debugging with Chrome DevTools
Jecelyn will share some tips and tricks to help you debug your web app effectively with Chrome DevTools.

Workshops on related topic

React, TypeScript, and TDD
React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.