PNPM

PNPM (Performant NPM) 📦

PNPM is a fast, disk space efficient package manager for Node.js. It was created as an alternative to npm and Yarn, focusing on performance and disk space optimization.

Architecture and Concepts 🏗️

PNPM's architecture is designed for efficiency and disk space savings:

  • 📁 Content-addressable store: Packages are stored in a global store, shared across projects

  • 🔗 Symlinks: Used to create the node_modules structure

  • 📄 package.json: Defines project dependencies and scripts

  • 🔒 pnpm-lock.yaml: Ensures consistent installs across machines

  • 📦 node_modules: Directory where packages are symlinked

Key Concepts:

  • 💾 Disk Space Efficiency: Saves disk space by using a single copy of each package version

  • 🚀 Fast Installation: Efficient algorithm for resolving and linking dependencies

  • 🌳 Non-flat node_modules: Prevents phantom dependencies

  • 🔄 Workspace Support: Manages multiple packages in a single repository

PNPM Architecture Diagram 📊

Common PNPM Commands 🖥️

Installation:

npm install -g pnpm

Initialize a new project:

pnpm init

Add a dependency:

pnpm add [package-name]
pnpm add [package-name]@[version]
pnpm add -D [package-name]  # Add as dev dependency

Remove a dependency:

pnpm remove [package-name]

Install all dependencies:

pnpm install

Run a script:

pnpm run [script-name]

Update packages:

pnpm update
pnpm update [package-name]

Code Snippets 💻

Example package.json:

{
  "name": "my-pnpm-project",
  "version": "1.0.0",
  "dependencies": {
    "express": "^4.17.1"
  },
  "devDependencies": {
    "typescript": "^4.5.4"
  },
  "scripts": {
    "start": "ts-node src/index.ts",
    "build": "tsc",
    "test": "jest"
  }
}

Example TypeScript file (src/index.ts):

import express from 'express';

const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello World from PNPM project!');
});

app.listen(port, () => {
  console.log(`Server running at <http://localhost>:${port}`);
});

Running the application:

pnpm start

Output:

Server running at <http://localhost:3000>

Deployment Commands 🚀

Here are some common deployment commands:

Build for production:

pnpm run build

Run tests before deployment:

pnpm test

Start in production mode:

pnpm start:prod

Add these scripts to your package.json:

{
  "scripts": {
    "build": "tsc",
    "test": "jest",
    "start:prod": "NODE_ENV=production node dist/index.js"
  }
}

Improved speed

The speed of package installation with pnpm is significantly better than npm and yarn. If you look at the below benchmark tests, you can see that pnpm performs better in most cases thano npm and yarn.

Conclusion 🎉

PNPM is a powerful and efficient package manager for Node.js projects. Its unique architecture provides significant disk space savings and improved performance. By leveraging PNPM's features and commands, you can optimize your development workflow and manage dependencies more effectively.

pnpm has overall performed much better than npm and yarn. No wonder giant tech companies like Vue3, Prism, and Microsoft are quickly adopting pnpm.

Last updated

Was this helpful?