pnpm介绍
动机
-
节省硬盘空间
不像npm那样,如果你一百个项目同时使用一个依赖,那npm会下载一百个副本在你的硬盘中。pnpm会将依赖存储在可循址的存储中(content-addressable store)- 如果你依赖了一个依赖的不同版本,只有版本之间差异的文件会被添加到存储中。
- 所有的文件都会被保存在硬盘的同一个地方。安装package时,它们的文件从该位置硬链接,不消耗额外的磁盘空间。
-
提高安装速度
pnpm 分三个阶段执行安装- 依赖解析
- 目录结构计算
- 链接依赖
创建非扁平node_modules目录结构
不像npm和yarn,会把所有目录提升到根目录中。导致源代码可以访问未作为依赖项添加到项目中的依赖项。
安装
命令行
管理依赖
pnpm add
安装依赖,默认为生产依赖
pnpm add sax保存到依赖dependence
pnpm add -D sax保存到开发依赖(devDependence)
pnpm add -O sax保存到可选依赖(optionalDependencies)
pnpm add -g sax安装全局依赖
pnpm add sax@next安装next版本
pnpm add sax@3.0.0安装指定版本pnpm install
别名:i
用于安装项目中所有的依赖
pnpm i --offline安装离线依赖
pnpm i --frozen-lockfile不更新pnpm-lock.yaml
pnpm i --lockfile-only只更新pnpm-lock.yamlpnpm update
别名:up,upgrade
更新依赖到指定范围的最新版本。
pnpm up更新所有依赖,采用package.json中指定的范围
pnpm up --latest所以依赖更新到最新版本
pnpm up foo@2更新foo到v2中的最新版本
pnpm up "@babel/*"更新所有@babel下的依赖pnpm remove
别名:rm,uninstall,un
移除依赖pnpm link
别名:ln
使当前本地包可在系统范围内或在其他位置访问。
pnpm link <dir>
pnpm link --global
pnpm link --global <pkg>
-
pnpm unlink
从另一个包管理器的锁定文件生成 pnpm-lock.yaml。支持的源文件有:- package-lock.json
- npm-shrinkwrap.json
- yarn.lock
pnpm rebuild
别名: rb
重新构建packagepnpm prune
移除无关的packagepnpm fetch
将包从锁定文件提取到虚拟存储中,包清单将被忽略。pnpm install-test
别名: it
运行 pnpm install之后立即运行 pnpm test。它采用与 pnpm install 完全相同的参数。pnpm dedupe
如果可以使用较新的版本,请执行安装,删除锁定文件中的旧依赖项。
补丁管理
-
pnpm patch <pkg>
准备一个补丁包
此命令将导致将包提取到可随意编辑的临时目录中。
Once you're done with your changes, run pnpm patch-commit <path> (with <path> being the temporary directory you received) to generate a patchfile and register it into your top-level manifest via the patchedDependencies field.
-
pnpm patch-commit <path>
Generate a patch out of a directory and save it (inspired by a similar command in Yarn).
This command will compare the changes from path to the package it was supposed to patch, generate a patch file, save the a patch file to patchesDir (which can be customized by the --patches-dir option), and add an entry to patchesDependencies in the top level manifest file.
-
pnpm patch-remove <pkg...>
Added in: v8.5.0
Remove existing patch files and settings in pnpm.patchedDependencies.
审查依赖
pnpm audit
检查已安装软件包的已知安全问题。pnpm list
别名: lspnpm outdated
检查过期包pnpm why
Shows all packages that depend on the specified package.pnpm licenses
运行脚本
-
pnpm run
别名: run-script
Runs a script defined in the package's manifest file.
-
pnpm test
Aliases: run test, t, tst
Runs an arbitrary command specified in the package's test property of its scripts object.
The intended usage of the property is to specify a command that runs unit or integration testing for your program.
-
pnpm exec
Execute a shell command in scope of a project.
node_modules/.bin is added to the PATH, so pnpm exec allows executing commands of dependencies.
-
pnpm dlx
Fetches a package from the registry without installing it as a dependency, hotloads it, and runs whatever default command binary it exposes.
For example, to use create-react-app anywhere to bootstrap a react app without needing to install it under another project, you can run:
pnpm dlx create-react-app ./my-app
This will fetch create-react-app from the registry and run it with the given arguments.
You may also specify which exact version of the package you'd like to use:
pnpm dlx create-react-app@next ./my-app
pnpm create
Create a project from a create-* or @foo/create-* starter kit.pnpm start
检查存储
-
pnpm cat-file
Added in: v8.13.1
Prints the contents of a file based on the hash value stored in the index file. For example:
-
pnpm cat-index
Added in: v8.13.1
Prints the index file of a specific package from the store. The package is specified by its name and version:
杂项
-
pnpm publish
Publishes a package to the registry.
pnpm [-r] publish [<tarball|folder>] [--tag <tag>]
[--access <public|restricted>] [options]
When publishing a package inside a workspace, the LICENSE file from the root of the workspace is packed with the package (unless the package has a license of its own).
You may override some fields before publish, using the publishConfig field in package.json. You also can use the publishConfig.directory to customize the published subdirectory (usually using third party build tools).
When running this command recursively (pnpm -r publish), pnpm will publish all the packages that have versions not yet published to the registry.
pnpm pack
Create a tarball from a package.pnpm -r, --recursive
Aliases: m, multi, recursive, <command> -rpnpm server
Manage a store server.pnpm store
Managing the package store.pnpm root
Prints the effective modules directory.pnpm bin
Prints the directory into which the executables of dependencies are linked.pnpm setup
This command is used by the standalone installation scripts of pnpm. For instance, in https://get.pnpm.io/install.sh.
Setup does the following actions:
creates a home directory for the pnpm CLI
adds the pnpm home directory to the PATH by updating the shell configuration file
copies the pnpm executable to the pnpm home directory
pnpm init
Create a package.json file.pnpm deploy
Deploy a package from a workspace. During deployment, the files of the deployed package are copied to the target directory. All dependencies of the deployed package, including dependencies from the workspace, are installed inside an isolated node_modules directory at the target directory. The target directory will contain a portable package that can be copied to a server and executed without additional steps.
Usage:
pnpm --filter=<deployed project name> deploy <target directory>
In case you build your project before deployment, also use the --prod option to skip devDependencies installation.
pnpm --filter=<deployed project name> --prod deploy <target directory>
Usage in a docker image. After building everything in your monorepo, do this in a second image that uses your monorepo base image as a build context or in an additional build stage:
# syntax=docker/dockerfile:1.4
FROM workspace as pruned
RUN pnpm --filter <your package name> --prod deploy pruned
FROM node:18-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY --from=pruned /app/pruned .
ENTRYPOINT ["node", "index.js"]
pnpm doctor
Checks for known common issues with pnpm configuration.pnpm configpnpm config
Aliases: c
Manage the configuration files.
The configuration files are in INI format.
The local configuration file is located in the root of the project and is named .npmrc.
The global configuration file is located at one of the following locations:
- If the $XDG_CONFIG_HOME env variable is set, then $XDG_CONFIG_HOME/pnpm/rc
- On Windows: ~/AppData/Local/pnpm/config/rc
- On macOS: ~/Library/Preferences/pnpm/rc
- On Linux: ~/.config/pnpm/rc
Commands
set <key> <value>
Set the config key to the value provided.get <key>
Print the config value for the provided key.delete <key>
Remove the config key from the config file.list
Show all the config settings.