PNPM: A Faster, Disk-Space Efficient Package Manager for JavaScript

Ramkumar Khubchandani
2 min readJul 8, 2024

--

PNPM (Performant NPM) is a package manager for JavaScript that offers significant improvements over its predecessor, npm. In this blog post, we’ll explore what pnpm is, how it differs from npm, and how you can migrate your projects from npm to pnpm.

What is PNPM?

PNPM is an alternative package manager for JavaScript projects. It aims to solve some of the issues associated with npm, particularly in terms of performance and disk space usage.

Key Differences Between PNPM and NPM

  1. Disk Space Efficiency:
  • NPM: Creates a new copy of each package for every project.
  • PNPM: Uses a content-addressable storage system, creating hard links to a single global content storage. This saves significant disk space.

2. Installation Speed:

  • NPM: Can be slower, especially for large projects.
  • PNPM: Generally faster due to its efficient storage system and optimized algorithms.

3. Package Hoisting:

  • NPM: Uses a flat node_modules structure, which can lead to “phantom dependencies”.
  • PNPM: Uses a nested node_modules structure, preventing access to packages not explicitly declared as dependencies.

4. Monorepo Support:

  • NPM: Requires additional tools for effective monorepo management.
  • PNPM: Has built-in support for monorepos with workspace features.

5. Lockfile:

  • NPM: Uses package-lock.json
  • PNPM: Uses pnpm-lock.yaml

Migrating from NPM to PNPM

Here’s how you can move your project from npm to pnpm:

  1. Install pnpm globally:
npm install -g pnpm

2. Remove the existing node_modules folder and package-lock.json:

rm -rf node_modules package-lock.json

3. Install dependencies using pnpm:

pnpm install

4. Update your scripts in package.json to use pnpm:

{
"scripts": {
"start": "pnpm run start",
"build": "pnpm run build",
"test": "pnpm run test"
}
}

5. If you’re using CI/CD, update your configuration files to use pnpm instead of npm.

Coding Example

Here’s a simple example of how to use pnpm in a project:

  1. Initialize a new project:
mkdir pnpm-example
cd pnpm-example
pnpm init

2. Install dependencies:

pnpm add express
pnpm add -D nodemon

3. Create a simple Express server (index.js):

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

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

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

4. Update package.json scripts:

{
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
}
}

5. Run the application:

pnpm run dev

Conclusion:

PNPM offers significant advantages over npm in terms of performance and disk space efficiency. By using a unique approach to package management, it solves many of the issues developers face with traditional package managers. Whether you’re starting a new project or considering migrating an existing one, pnpm is definitely worth considering for your JavaScript package management needs.

Do You want to learn more like above content ?

Follow me or message me on Linkedin.

--

--

Ramkumar Khubchandani
Ramkumar Khubchandani

Written by Ramkumar Khubchandani

Frontend Developer|Technical Content Writer|React|Angular|React-Native|Corporate Trainer|JavaScript|Trainer|Teacher| Mobile: 7709330265|ramkumarkhub@gmail.com

No responses yet