SoFunction
Updated on 2025-03-03

Installation and usage tutorial of pnpm (summary)

1. What is pnpm

pnpm  (performance npm, meaning high-performance npm) is an alternative package manager. It is a direct alternative to npm, faster and more efficient. Why is it more efficient? When you install a package, pnpm saves it in a global storage on your machine, and we then create a hard link from it instead of copying. For each version of the module, only one copy is saved on disk.

For example, when using npm or yarn, if you have 100 packages using lodash, there will be 100 copies of lodash on disk. Pnpm allows you to save gigabytes of disk space!

It is derived from npm/yarn, but it solves potential bugs inside npm/yarn and greatly optimizes performance

2. What are the characteristics

(1) Fast speed

The official benchmark data is like this, but in actual use, it was found that the first download package of pnpm is similar to that of yarn. Its advantage is that the second download of the same package is faster.

(2) Efficient use of disk space

pnpm uses a content-based file system to store all files on disk internally. The excellent thing about this file system is that it is
1. The same package will not be installed repeatedly. When using npm/yarn, if 100 projects rely on lodash, then lodash is likely to be installed 100 times, and 100 places on the disk have written this part of the code. However, when using pnpm, it will only be installed once, and there is only one place to write to it on disk. If you use it again, you will use hardlink directly (hardlink)
2. Even if a package has different versions, pnpm will greatly reuse the previous version of the code. For example, if lodash has 100 files and one more file is added after the update version, then 101 files will not be re-written on the disk, but the original 100 files will be retained, and only the newly added file will be written.

(3) *Support monorepo

As front-end engineering becomes increasingly complex, more and more projects are starting to use monorepo. In the past, for the management of multiple projects, we generally used multiple git repositories, but the purpose of monorepo was to use a git repository to manage multiple subprojects. All subprojects are stored in the packages directory of the root directory, so a subproject represents a package.

3. PNPM Dependence Principle

Problems with npm and yarn installation packages

Before the emergence of pnpm, both npm and yarn adopted flat packaging strategies in order to improve the reuse rate of packages. The flat installation method will cause a big difference between our node_modules folder, such as if you install a package express, but there will be many packages under your node_modules.
There will be some problems at this time

(1) Ghost dependence

From the current package reference method, when importing, we will look for it from the node_modules folder. According to the figure above, if we do not have accepts in it, we can actually refer to it, because it does exist. At this time, we are accessing the npm package without declaring it. If one day the express main package no longer depends on accepts, the project will have a problem of dependency missing. We use this subpackage of the main package dependency in the project without being declared, which can be understood as a package that is entrained by the main package, which we call it ghost dependency.

(2) Uncertainty of package version

This is easy to understand. If both the main packages A and B depend on the accepts package, but A depends on [email protected] and B depends on [email protected], then will the flat structure under node_modules show 1.0 or 2.0? The current method is to display whoever installs it. There are many problems caused by this uncertainty in development. "Others can solve this problem with this package, but I cannot solve it if I install this package." This is often the reason.

(3) Rely on repeated installation

This is also easy to understand. AB all relies on accepts and depends on different versions. No matter which version is upgraded to the top layer of node_modules, this package will be installed twice.

4. Installation and use of pnpm

1. Installation

// Global installationnpm install pnpm -g

2. View the source

pnpm config get registry 

3. Switch Taobao source

pnpm config set registry /

4. Use

pnpm install Bag // Installation dependenciespnpm i  Bag      // Installation dependenciespnpm add Bag     // -S default write dependenciespnpm add -D     // -D devDependencies
pnpm add -g     // Global installation

5. Remove

pnpm remove Bag               // Remove the packagepnpm remove Bag --global      // 移除全局Bag

6. Update

pnpm up                      // Update all dependenciespnpm upgrade Bag              // Update packagepnpm upgrade Bag --global     // 更新全局Bag

Note: pnpm will not automatically download other dependencies used in dependencies. npm and cnpm can automatically download other dependencies in dependencies. This involves the issue of dependency chain. pnpm will first check the shared repository. If there is this dependency, create a symbolic link (i.e., a hard link) to locate the dependencies in the shared repository and will not download it repeatedly. npm is a dependency for each project to be downloaded separately.

7. If you want to automatically download dependencies, you can use

pnpm recursive install

8. pnpm also provides some other commands to manage dependencies, such as:

pnpm recursive add <package>:Add dependencies to the project and its subprojects
pnpm recursive update:Update all dependencies

Finally, the official website address is attached:Motivation | pnpm

Replenish:

Installation and use of pnpm (most detailed in the entire network)

pnpm is a disk-efficient package manager.
When using npm or Yarn, if you have 1000 projects and all projects have the same dependency package, then you need to save 100 copies of the same dependency package on your hard drive. However, if you use pnpm, the dependent package will be stored in a unified location. Therefore, in terms of the proportion of projects and dependent packages, using pnpm will save a lot of hard disk space and the installation speed can be greatly improved. At present, pnpm has been released in version 6.8.0. This version contains many new features, performance optimizations and bug fixes. The main updates are as follows:

  • pnpm is said to be nearly 2 times faster than similar tools
  • All files in node_modules are linked from a single storage location
  • pnpm has built-in support for multiple packages in a single source repository
  • node_modules created by pnpm are not flat structures, so the code cannot access any software packages
  • pnpm: official website portal/zh/motivation

1. Install first

Node environment (✔️)
npm environment (✔️)

2. Install pnpm

npm install -g pnpm

Setting up the mirror source

pnpm config set registry /
# examinepnpm config get registry

Commonly used commands

# View all versions of ts-nodepnpm view ts-node versions
# Equivalent to npm i nodemon -gpnpm add nodemon -g
# npm i
pnpm i
# View dependencies (global)pnpm list [-g]
# The best use is: npm run dev/test/buildpnpm dev
# Of course habitnpmIt can also be used pnpm run dev

This is the end of this article about the installation and use of pnpm. For more related content on installation and use of pnpm, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!