I. Overview
1. Smaller.
When using npm, dependencies will be installed repeatedly every time they are used by different projects. When using pnpm, dependencies are stored in content-addressable storage.
2. Faster
- Dependency resolution. Dependencies that are not in the warehouse are identified and fetched to the warehouse.
- Directory structure calculation.
node_modules
The directory structure is calculated based on dependencies. - Link dependencies. All previously installed dependencies will be fetched directly from the repository and linked to
node_modules
.
3. Flattening
When using npm or Yarn Classic to install dependencies, all packages are elevated to the root directory of the module directory. This leads to a problem, the source code can directly access and modify the dependencies, rather than as read-only project dependencies.
By default, pnpm uses symbolic links to add the project's direct dependencies to the root of the module directory.
You can see that when referenced
cookie
inexpress
, only hard links are stored in dependencies, and the real packages are in the flattened first-level directory.
2. Installation
npm
Based on installation under windows :
npm i -g pnpm
- Use a separate script to install under windows:
iwr https://get.pnpm.io/install.ps1 -useb | iex
//默认安装路径:`C:\Users\【用户名】\AppData\Local\pnpm`
//如果想要指定安装目录,请提前设置环境变量:【PNPM_HOME】=[指定目录]
3. Configuration
pnpm sets up configurations in the same way as npm. And npm
the configuration that is directly inherited, for example,
npm config list
pnpm config list
pnpm config get <key>
pnpm config set <key>
pnpm config delete <key>
As you can see, list
the print results of the two commands are exactly the same.
pnpm config list
pnpm config get <key>
pnpm config set <key>
pnpm config delete <key>
4. Filtering
Match: filter
Filtering allows you to limit commands to a specific subset of packages. Selectors can be specified via the --filter
(or ) flag:-F
pnpm -F <package_name> start
pnpm --filter <package_name> start
The above command will enter project1
the project and execute pnpm run start
the command.
exclude:!
Any filter selector can be used as an exclusion by adding a "!" at the beginning.
For example, this will foo
run a command in all projects except :
pnpm --filter=!foo <cmd>
5. pnpm:devPreinstall
Script
pnpm:devPreinstall
is a special lifecycle script that is pnpm install
run when a command is executed. This script is usually used to perform some custom operations before installing dependencies, such as a build or configuration process.
Open package.json
the file and add the following code in scripts
the section:
{
"scripts": {
"pnpm:devPreinstall": "your-command-here"
}
}
6. Management dependence
1. Installation: pnpm add
pnpm add <pkg> //保存到 dependencies
pnpm add <pkg> -D //保存到 devDependencies
pnpm add <pkg> -O //保存到 optionalDependencies
pnpm add <pkg> -g //安装到全局
pnpm add <pkg>@next //从 next 标签下安装
pnpm add <pkg>@latest //从 latest 标签下安装
pnpm add <pkg>@3.0.0 //安装指定版本 3.0.0
//从本地安装
pnpm add ./package.tar.gz
pnpm add ./some-directory
//从远端安装 Tar 包
pnpm add https://github.com/indexzero/forever/tarball/v0.5.6
//从 git 安装
pnpm add <git remote url>
Configuration item description:
-
--save-prod, -P
Install the specified package as regulardependencies
. -
--save-dev, -D
Install the specified packages asdevDependencies
. -
--save-optional, -O
Install the specified packages asoptionalDependencies
. -
--save-exact, -E
Saved dependencies will be specified as an exact version, rather than using pnpm's default semver range operator configuration. -
--save-peer
Using --save-peer will add one or morepeerDependencies
packages and install them todev dependencies
. -
--ignore-workspace-root-check
Use--ignore-workspace-root-check
or-w
to add dependencies to the workspace root directory. For example,pnpm add debug -w
. -
--global, -g
Install global dependencies -
--workspace
Only add dependencies found in the workspace.
When installing some command line dependencies globally,
bin
the files corresponding to the command linepnpm
will be copied toPNPM_HOME
the directory corresponding to the environment variable by default. For examplepnpm add express express-generator -g
, after executing:, the file corresponding to the express command line will appear in the root directorybin
:
2. Installation: pnpm install
is equivalent pnpm i
to installing all dependencies of the project.
3. Update: pnpm update
alias: up
, upgrade
. Updates the latest version of a package based on the specified scope. All dependencies are updated by default.
Command | Meaning |
---|---|
pnpm up |
package.json Update all dependencies according to the specified scope |
pnpm up --latest |
Updates all dependencies, this operation ignores package.json the specified scope |
pnpm up foo@2 |
Update foo to the latest version on v2 |
pnpm up "@babel/*" |
Update @babel all dependencies in scope |
4. Delete: pnpm remove
aliases: rm
, uninstall
, delete related items un
from node_modules
and project .package.json
packages
5. Connection: pnpm link
alias: ln
Make the current local package accessible system-wide or elsewhere.
pnpm link <dir>
pnpm link --global
pnpm link --global <pkg>
6. Unlink: pnpm unlink
unlinks a system-wide package
(relative to pnpm link
).
7. Rebuild: pnpm rebuild
alias: rb
Rebuild one package
.
8. Pruning: pnpm prune
removes unnecessary ones packages
.
9. Modify the global package directory.
Execute the following command, and then all dependent packages will be saved in the specified directory.
pnpm config set store-dir D:\Node\Cache\pnpm\store
https://pnpm.io/zh/next/cli/store
7. View dependencies
1. Check: pnpm audit
Check installed packages for known security issues.
In fact, when using this command, the Taobao mirror (https://registry.npmmirror.com/) does not support checking, and the following error will be prompted:
When using npm
the official warehouse, it is possible, as shown below:
2. Printing dependencies: pnpm list
Alias:ls
This command will output all installed package
versions and their dependencies in a tree structure.
pnpm ls
pnpm ls -r //递归打印工作区依赖
pnpm ls --depth=0 -r
pnpm ls -g --depth=0
3. Print expired dependencies:
The pnpm outdated command is quite practical and can check package.json
the current and latest versions of all dependent packages.
For example:
common1
The above common2
is a custom module in the workspace, so it cannot be detected, and the others are recorded.
4. Print the dependencies of the specified package: pnpm why
displays package
all dependencies on the specified package package
.
For example, the following prints out all dependent packages in the workspace common2
. umi-antd
They are projects and projects respectively web/common1
.
8. Run the script
1. Run: pnpm run
If you have a watch
script configured in package.json
, like this:
"scripts": {
"watch": "webpack --watch",
"watch1": "webpack --watch",
"watch3": "webpack --watch",
}
Execute pnpm run watch
.
You can also use regular expressions to match and execute multiple scripts. The following command will match all watch
starting commands.
pnpm run "/^watch:.*/"
2. Execution: pnpm exec
executes shell commands within the project scope.
For example, node_modules/.bin
there is a max
script file in the directory under the project directory. Direct execution max -v
is invalid, but pnpm exec max -v
this command can be executed through.
pnpm exec [命令名]
Similar tonpx [命令名]
3. Create a project: pnpm dlx/pnpm create.
Both commands can be used to create a project. For example, create a react project:
pnpm dlx create-react-app ./my-app
pnpm create create-react-app ./my-app
The above two commands have the same execution effect. They can both initialize a react project and dependency packages are installed by default.
Only pnpm dlx
the command supports some configuration items.
9. Manage Node versions
1. Configuration command
pnpm env <cmd>
//cmd:
//use:使用
//remove:删除
//list:打印全部
Here is an introduction to the configuration that needs to be done under window :
- Clear the settings
PATH
under [Environment Variables]node
- Install system-independent scripts. pnpm installation tutorial on other platforms
//window下打开powershell:
iwr https://get.pnpm.io/install.ps1 -useb | iex
-
View available versions
pnpm env ls --remote
-
Install the required version
pnpm env use -g lts //安装LTS 版本 pnpm env use -g 16 //安装 v16: pnpm env use -g latest //最新版本
-
Remove a specific version:
pnpm env remove -g 14.0.0
pnpm
Globally installed packages are saved in the: C:\Users\【用户名】\AppData\Local\pnpm
directory.
2. Practical drills
After installing the standalone script version pnpm
.
- Install stable version: execute
pnpm env use -g lts
-
Remove stable version:
pnpm env rm -g lts
-
Check if removal is successful:
node -v
At this time, the node version cannot be detected, indicating that the deletion was successful.
I have installed and modified it before .
npm
After reinstalling the independent script, there is no change before and I continue to use it.cache
prefix
pnpm
npm config
Like pnpm!
10. Cache directory
1 Overview
- npm global installation
npm i -g lodash
After execution, lodash
it is stored in npm config get prefix
the corresponding directory.
For example:
- pnpm global installation
pnpm install -g lodash
After execution is completed, the package is stored in C:\Users\【用户名】\AppData\Local\pnpm
the directory :
2. Modify the pnpm cache address
- Modify
store
directory:
pnpm config set store-dir [目录]
pnpm store path #查看store目录
- Modify
cache
directory:
cache
cache is stored in by defaultC:\Users\【用户名】\AppData\Local\pnpm-cache
. Add the environment variable [XDG_CACHE_HOME=D:\Node\Cache\pnpm\cache
], and the cache of dependencies installed later will be stored in this directory. - Remove unreferenced packages from storage.
pnpm store prune
Recommended reading: pnpm official documentation