> For the complete documentation index, see [llms.txt](https://www.ankitavirani.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.ankitavirani.com/experience/packege-mangers/pnpm.md).

# 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 📊

<figure><img src="/files/sBd6vBBuwYM7JVpPVuiZ" alt=""><figcaption></figcaption></figure>

### Common PNPM Commands 🖥️

#### Installation:

```bash
npm install -g pnpm
```

#### Initialize a new project:

```bash
pnpm init
```

#### Add a dependency:

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

#### Remove a dependency:

```bash
pnpm remove [package-name]
```

#### Install all dependencies:

```bash
pnpm install
```

#### Run a script:

```bash
pnpm run [script-name]
```

#### Update packages:

```bash
pnpm update
pnpm update [package-name]
```

### Code Snippets 💻

#### Example package.json:

```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):

```tsx
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:

```bash
pnpm start
```

Output:

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

### Deployment Commands 🚀

Here are some common deployment commands:

#### Build for production:

```bash
pnpm run build
```

#### Run tests before deployment:

```bash
pnpm test
```

#### Start in production mode:

```bash
pnpm start:prod
```

Add these scripts to your package.json:

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

#### Improved speed[​](https://refine.dev/blog/pnpm-vs-npm-and-yarn/#improved-speed) <a href="#improved-speed" id="improved-speed"></a>

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.

<figure><img src="https://refine.ams3.cdn.digitaloceanspaces.com/blog/2022-10-13-pnpm-post/pnpm-image-4.png" alt=""><figcaption></figcaption></figure>

### 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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.ankitavirani.com/experience/packege-mangers/pnpm.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
