w3resource

How to Use PNPM with Node.js: Complete Guide


Using PNPM with Node.js

PNPM is an advanced and efficient package manager for Node.js, similar to npm and Yarn. It is optimized for performance and disk space by using a content-addressable storage system. PNPM links dependencies instead of duplicating them, making installations faster and reducing disk usage. This guide explores how to install and use PNPM in Node.js projects, complete with examples, explanations, and best practices.


Syntax:

    1. Install PNPM globally:

    npm install -g pnpm
    

    2. Use PNPM commands to manage packages:

    • Install dependencies: pnpm install
    • Add a package: pnpm add <package-name>
    • Remove a package: pnpm remove <package-name>

Examples and Code

1. Installing PNPM

To use PNPM as your Node.js package manager, install it globally using npm:

# Install PNPM globally
npm install -g pnpm

Verify PNPM Installation:

# Check PNPM version
pnpm --version

Output:

8.15.0

2. Initialize a Node.js Project Using PNPM

Run the following command to initialize a new Node.js project:

# Create a new Node.js project
pnpm init

This will generate a package.json file.


3. Installing Dependencies with PNPM

Install All Dependencies

If your project has a package.json file with dependencies, use:

# Install all dependencies from package.json
pnpm install

PNPM will install all packages efficiently without duplicating them.

Add a Single Package

To install a new package (e.g., Express):

# Add Express package
pnpm add express

Remove a Package

To remove a package (e.g., Express):

# Remove Express package
pnpm remove express

4. Running Node.js Project with PNPM

Create a simple server using Express to demonstrate PNPM usage.

Folder Structure

project/
│
├── index.js           # Node.js main file
├── package.json       # Dependencies file
└── pnpm-lock.yaml     # PNPM lock file

index.js (Node.js Server)

Code:

// Import Express (installed via PNPM)
const express = require('express');

// Create an Express app
const app = express();

// Define a route for the server
app.get('/', (req, res) => {
    res.send('Hello from Node.js and PNPM!');
});

// Start the server on port 3000
const PORT = 3000;
app.listen(PORT, () => {
    console.log(`Server running at http://localhost:${PORT}`);
});

Run the Project:

To start the server:

Code:

node index.js

Visit http://localhost:3000 to see the output:

Code:


Hello from Node.js and PNPM!

5. Advantages of Using PNPM

    1. Disk Space Efficiency: PNPM uses a global content-addressable store to avoid duplicating dependencies.

    2. Faster Installations: The linking process speeds up dependency installation.

    3. Strict Dependency Isolation: PNPM enforces strict rules for dependencies, ensuring cleaner project management.

    4. Support for Monorepos: PNPM has built-in support for monorepos, simplifying large-scale project management.


Comparison: PNPM vs npm vs Yarn

Feature PNPM npm Yarn
Performance Fast Moderate Fast
Disk Usage Minimal High Moderate
Dependency Linking Hard links Copies files Copies files
Monorepo Support Built-in Limited Partial
Lock File pnpm-lock.yaml package-lock.json yarn.lock

Best Practices with PNPM

    1. Always Use a Lock File: Commit pnpm-lock.yaml to ensure consistent dependencies.

    2. Avoid Mixed Package Managers: Stick to PNPM throughout the project for consistency.

    3. Enable Node.js Compatibility: Use PNPM for monorepos and modular projects to maintain clean dependency structures.


Common PNPM Commands

Command Description
pnpm install Install all dependencies.
pnpm add <package> Install a specific package.
pnpm remove <package> Remove a specific package.
pnpm update Update all dependencies.
pnpm list List all installed dependencies.
pnpm run <script> Run a script from package.json.


Follow us on Facebook and Twitter for latest update.