Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Introduction to Using pnpm

Introduction to Using pnpm

As a Node.js developer, you must have heard of npm and yarn, which are great tools for managing project dependencies. But have you ever run into the trouble of node_modules an enormous folder, slow installation, or disk space being quickly filled up? This is the problem pnpm can solve.

pnpm is a fast, disk-space-efficient Node.js package manager. It stores packages using content addressing, with all versions of dependencies stored centrally in one location on the system, then linked into projects via hard links, avoiding duplicate installation.

pnpm's slogan is Fast, disk space efficient package manager(fast, disk-space-efficient package manager). It is not only extremely fast, but also saves you a large amount of disk space and bandwidth through its unique linking mechanism.


Installing pnpm

There are several ways to install pnpm; you can choose according to your preferences and system environment.

Method 1: Install using npm (recommended for all users)

This is the most universal and simplest method. Open your terminal (Windows users can use PowerShell or CMD) and run the following command:



npm install -g pnpm

This command will install pnpm into your global environment via npm.

to verify the installation succeeded: After installation, run the following command to check the version. If the version number displays normally, the installation was successful.



pnpm --version

Output similar to: 8.15.0

Method 2: Install using a standalone script

For macOS or Linux users, you can install using the following script:



# macOS/Linux

curl -fsSL https://get.pnpm.io/install.sh | sh -



# Windows (PowerShell)

iwr https://get.pnpm.io/install.ps1 -useb | iex


Using pnpm

Now that you've learned how to install it, let's start using pnpm in practice. Its commands are designed to be very intuitive; if you're familiar with npm, you can switch almost seamlessly.

Initialize a new project

Similar to npm init, pnpm can be used to initialize a project and create package.json files.



# Interactively answer questions, create package.json

pnpm init



# Quickly create a package.json with default values

pnpm init -y

Run pnpm init -y , you'll get the most basic package.json File:

Example

{

 

"name"

:

"your-project-name"

,

 

"version"

:

"1.0.0"

,

 

"description"

:

""

,

 

"main"

:

"index.js"

,

 

"scripts"

:

{

   

"test"

:

"echo "Error: no test specified" && exit 1"

 

}

,

 

"keywords"

:

[

]

,

 

"author"

:

""

,

 

"license"

:

"ISC"

}

Install dependencies

pnpm's install command is pnpm add , equivalent to npm install 。



# Install all dependencies

pnpm install



# Add dependencies

pnpm add 



# Add development dependencies

pnpm add -D 



# Add global package

pnpm add -g 



# Install specified version

pnpm add @

Update dependencies

# 更新所有依赖

pnpm update



# Update specified package

pnpm update 



# Update to the latest version (ignoring version ranges in package.json)

pnpm update --latest

Removing dependencies

pnpm remove 

Running scripts

# 运行 package.json 中定义的脚本

pnpm run 

# Some common abbreviations

pnpm start

pnpm test

pnpm build

npm to pnpm command comparison table

npm command pnpm command
npm install pnpm install
npm install pnpm add
npm install --save-dev pnpm add -D
npm install --global pnpm add -g
npm update pnpm update
npm uninstall pnpm remove
npm run