18, Sep 2022 ~ 5 min read

PNPM for the impatient

PNPM is a great alternative to yarn, and truly Blazingly Fast

Listen to something with me 😍

Want to listen somewhere else? 🎧

Table of Contents

Top

Dependency Management

I’d doubt anyone who has been working professionally in the Web Development space and worked with NodeJS doesn’t use npm at some point. Everyone in the JavaScript ecosystem has at least heard about npm. Interesting fact, npm is a separate project it just ships with a fresh install of NodeJS by default. If you’ve used NodeJS under a Linux box, you might have installed it separately.

It’s a great tool, it’s useful for managing project dependencies but sometimes it get’s hard to work with. Some of those issues with NPM

  • Space: NPM takes so much space
  • Performance: NPM is notorious for being slow
  • Security: Due to how NPM manages bad packages has a history of having security vulnerabilities that have affected many projects
  • Workspaces: We’ll get to this but missing features for managing workspaces using NPM

Top

Alternatives for NPM

NPM is something I use everyday, especially for work, however for personal project’s I started trying out other package managers. I enjoyed using yarn, it was faster than NPM and was stable, secure until I tried PNPM. I was instantly shocked by the speed, it boasts PNPM is 2x faster than NPM but I think it might even be more faster. I personally loved PNPM, I continued using PNPM and still haven’t looked back.

Top

Why PNPM?

I Wanted to share some of the features of PNPM, I consider them benefits really, so I’ll list some features that really stood out to me

  1. Speed: PNPM is very fast, it’s stable and I love it.
  2. API: PNPM is a CLI application, just like NPM, and it’s very similar to NPM unlike yarn. It has some improvements over NPM like you can invoke script commands without having to write pnpm run every time.
  3. Space Efficiency: PNPM doesn’t copy cached packages, if it’s already cached it simply creates a symlink for the package each time it’s used. Essentially, having only one instance of that dependency.
  4. Workspace Management: We’ll talk about workspaces in depth but in general, it has additional features for managing workspaces.

Top

Workspaces, using PNPM

In version control systems, a monorepo is a software development strategy where code for many projects is stored in the same repository. This software engineering practice dates back to at least the early 2000s, when it was known as a ‘shared codebase’. Wikipedia

If you’re not familiar with monorepos, it’s a technique for having all you’re related projects in one repository so during development it’s easier to reuse, share and update them across teams. I’ve been interested in monorepos for a while now. I’m currently using a monorepo in my current personal project, in my opinion I think it’s better to have a built in tool to handle projects in a monorepos. PNPM has exactly that, it does a good job handling my monorepo setup I don’t need to look for another tool.

A workspace is synonymous with a directory containing multiple NodeJS packages, and a package is a single NodeJS project that has a package.json file. Here are some of the features of PNPM for managing monorepos,

  1. Workspace Protocol: If you’re going to target specifically a package within your workspace, you can directly state "foo": "workspace:*"
  2. Filtering Packages: PNPM has the ability to filter and run commands. This gives you the ability to Match packages of a certain keyword, exclude packages, filter only dependencies. You can apply one or more filters to most of the commands, like install, run and rebuild.
  3. Configuration: PNPM has a workspace configuration file, you can configure your workspace using a file called pnpm-workspace.yaml. You can configure the list of root directories for your packages, and exclude directories using a pattern string.

Top

Basic Workspace Setup using PNPM

mkdir ./packages
touch ./pnpm-workspace.yaml

Add packages directory as a root directory for packages,

packages:
  - 'packages/*'

Now you can configure your package.json scripts to execute commands on these packages, this is an example from the webpack-or-legacy article series project I am working on my blog

{
  "name": "webpack",
  "version": "0.1.0",
  "description": "A simple brain storming app, built using webpack",
  "main": "src/index.js",
  "scripts": {
    "build": "pnpm -r build",
    "dev": "pnpm -r --parallel dev"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/qeberodev/webpack-or-legacy.git"
  },
  "keywords": [],
  "author": "Zablon Dawit, zablon@qebero.dev",
  "private": "true",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/qeberodev/webpack-or-legacy/issues"
  },
  "homepage": "https://github.com/qeberodev/webpack-or-legacy#readme",
  "devDependencies": {
    "css-loader": "^6.7.1",
    "html-webpack-plugin": "^5.5.0",
    "mini-css-extract-plugin": "^2.6.1",
    "sass": "^1.54.5",
    "sass-loader": "^13.0.2",
    "webpack": "^5.74.0",
    "webpack-cli": "^4.10.0",
    "webpack-dev-server": "^4.10.0"
  }
}

Basically, this is it to get working on your monorepo project.

Top

Managing Multiple NodeJS versions

I’ve been working on a project, and it requires a specific version of NodeJS, most of the time an older version. The one thing you can do if you’re using Linux is install something like NVM to manage multiple versions of NodeJS. Well, PNPM can manage multiple versions of NodeJS. I don’t use PNPM for that purpose, just because I have already had another version manager for setup.

pnpm env use --global lts
pnpm env use --global latest
pnpm env use --global 16

Top

Conclusion

PNPM is a great alternative to NPM especially if you’re looking for something very similar in API but is faster, efficient and is a bit more comfortable to work with. I really enjoy using PNPM personally, if you’re thinking of using something like yarn you might want to try out PNPM too. Thank you for reading.

Top

References

Top


Headshot of Maxi Ferreira

Hi, I'm Zablon. I'm a software engineer, mostly working in the web space, based in Ethiopia. You can follow me on Twitter, see some of my work on GitHub, or read more about Qebero.dev on here.