pnpm: Streamlining JavaScript Development in Conjunction with Git Worktree

Akshay N
5 min readNov 26, 2023

Introduction

In the dynamic world of software development, efficiency and resource management are key. Following my recent exploration of Git Worktree, a powerful yet underutilized feature for managing multiple branches, I now turn my attention to pnpm - a fast, disk space-saving package manager for JavaScript. This blog post delves into the advantages of pnpm, especially when used in tandem with Git Worktree.

What is pnpm?

pnpm stands for "performant npm" and is a package manager for JavaScript that aims to improve upon the shortcomings of npm and Yarn. It's designed to offer faster installations and significant disk space savings, making it an ideal choice for developers working on multiple projects or large codebases.

Key Advantages of pnpm

  1. Efficient Disk Space Usage: Unlike npm or Yarn, pnpm creates a single copy of a module and links it wherever it's needed. This symlink approach reduces redundancy and saves disk space.
  2. Speed: pnpm installations are generally faster, thanks to its efficient handling of dependencies and modules.
  3. Strictness: It offers a more strict module resolution, which avoids accidental usage of undeclared dependencies.

pnpm and Git Worktree: A Perfect Pair

When combined with Git Worktree, pnpm shines even brighter. Git Worktree allows developers to check out multiple branches in separate directories and pnpm complements this by ensuring that each worktree uses minimal additional disk space for node_modules. This synergy is particularly beneficial for JavaScript developers who frequently switch between branches and work on different features or bugs.

A Practical Scenario

Imagine you’re using Git Worktree to manage multiple branches of a JavaScript project. Each branch checked out in a separate directory, requires its own node_modules. With npm or Yarn, this would mean duplicating dependencies across directories, consuming significant disk space. pnpm changes the game here by creating symlinks to a single version of each module, drastically reducing the space required.

Getting Started with pnpm

To start using pnpm, you can install it globally via npm:

npm install -g pnpm

Then, in your project, you can run:

pnpm install

This will set up the node_modules in a way that’s optimized for space and performance.

Understanding the --shamefully-hoist Flag in pnpm

One of the unique features of pnpm is its approach to handling node_modules. Unlike npm or Yarn, pnpm creates a non-flat node_modules structure by default, which means it doesn't place all dependencies at the root level of node_modules. This approach is more efficient and mirrors the actual dependency tree, but it can sometimes lead to issues with packages that expect a flat node_modules structure.

This is where the --shamefully-hoist flag comes into play.

What Does --shamefully-hoist Do?

The --shamefully-hoist flag is a workaround for compatibility issues. When used, it makes pnpm behave more like npm or Yarn in terms of the node_modules structure. It hoists (or lifts) all dependencies to the root of the node_modules directory, creating a flat structure.

Advantages of Using --shamefully-hoist

  1. Compatibility: Some tools and packages, especially older ones, may expect a flat node_modules structure. Using --shamefully-hoist ensures compatibility with these tools.
  2. Simpler Debugging: A flat node_modules structure can sometimes make it easier to locate and debug issues with specific packages.
  3. Ease of Transition: For teams transitioning from npm or Yarn to pnpm, using --shamefully-hoist can make the switch smoother, as the structure of node_modules remains familiar.

How to Use --shamefully-hoist

To use this flag, simply add it to your pnpm install command:

pnpm install --shamefully-hoist

Or, you can add it to your .npmrc or pnpm-workspace.yaml file to apply it to all installations:

shamefully-hoist=true

A Balanced Approach

While --shamefully-hoist offers compatibility and ease of use, it's important to remember that it deviates from pnpm's core philosophy of efficient and accurate dependency management. Therefore, it's recommended to use this flag only when necessary for compatibility reasons.

Using pnpm Alongside npm: A Practical Shell Script

For many development teams, npm remains the primary package manager due to its widespread adoption and familiarity. However, the benefits of pnpm are too significant to ignore. If you're looking to integrate pnpm into your workflow while keeping npm as the main package manager, here's a handy tip. I've crafted a small shell script that allows you to use pnpm as a secondary package manager, preserving the project structure for other contributors who might still be using npm.

The Shell Script

This script ensures that pnpm can be used without disrupting the existing npm-based workflow:

pnpm import
pnpm i --shamefully-hoist
rm -rf pnpm-lock.yaml

Let’s break down what each line does:

  1. pnpm import: This command imports an existing package-lock.json file and generates a pnpm-lock.yaml file. It ensures that pnpm respects the versions locked in your npm project.
  2. pnpm i --shamefully-hoist: As discussed earlier, this command installs dependencies using pnpm while hoisting them to mimic a flat node_modules structure, ensuring compatibility with tools expecting an npm-like structure.
  3. rm -rf pnpm-lock.yaml: After installation, this script removes the pnpm-lock.yaml file. It's a crucial step to prevent conflicts or confusion among team members who are not using pnpm. This way, the package-lock.json remains the single source of truth for your project's dependencies.

Advantages of This Approach

  • Flexibility: This script allows you to leverage pnpm's efficiency and speed without forcing the entire team to switch over.
  • Compatibility: It maintains the project’s structure as if it were managed solely by npm, ensuring no disruptions in the team’s workflow.
  • Best of Both Worlds: You get to enjoy the disk space and performance benefits of pnpm while keeping the project accessible for npm users.

Conclusion

pnpm represents a significant step forward in JavaScript package management, especially when used in conjunction with Git Worktree. Its efficient handling of dependencies not only saves disk space but also enhances the overall development experience. As the software development landscape continues to evolve, tools like pnpm and Git Worktree will be invaluable for developers looking to optimize their workflows.

Integrating pnpm into your development process doesn't have to be an all-or-nothing decision. With this simple shell script, you can enjoy the benefits of pnpm alongside npm, ensuring a smooth and efficient workflow that respects the preferences and needs of your entire team.

--

--

Akshay N
Akshay N

Written by Akshay N

Skilled Software Developer & DevOps pro. Quick learner, problem-solver, team player. Passionate about efficient, innovative tech solutions.

No responses yet