全局软件包
全局软件包是使用 pnpm add -g 安装到系统范围内的 CLI 工具和实用程序。
安装全局软件包#
pnpm add -g <pkg>
例如:
pnpm add -g typescript prettier eslint
注意
请勿通过 sudo 运行这些命令。 pnpm keeps global packages and configuration in the invoking user's home directory. Commands that modify the global installation fail under sudo with ERR_PNPM_SUDO_NOT_SUPPORTED; read-only commands such as pnpm bin -g are unaffected.
独立安装#
每个全局安装的软件包(或一起安装的一组软件包)都有其自己的独立安装目录,其中包含自己的 package.json、node_modules/ 和锁文件。这样可以阻止全局包之间因对等依赖冲突、提升更改或版本错位而相互干扰。
隔离安装存储在 {pnpmHomeDir}/global/v11/{hash}/,其中哈希值是从一起安装的软件包集合中派生出来的。
例如,运行以下两个命令:
pnpm add -g typescript
pnpm add -g prettier
creates two separate isolated installations. typescript and prettier each get their own node_modules tree and cannot affect each other's dependency resolution.
在单个命令中安装多个空格分隔 软件包也会为每个软件包单独创建一个单独的安装:
pnpm add -g eslint prettier
eslint and prettier each get their own node_modules tree and lockfile and can be removed independently. pnpm remove -g eslint leaves prettier untouched.
To bundle multiple packages into the same isolated install (so they share a node_modules tree and lockfile, resolve peer dependencies against each other, and are removed together), pass them as a comma-separated list:
pnpm add -g eslint,prettier
这里的 eslint 和 prettier 形成了一个安装组。使用 pnpm remove -g 删除任一项都会删除整个组。
pnpm add -g <pkg> replaces every existing group that contains <pkg>. After pnpm add -g eslint, the eslint,prettier group is gone, and so are prettier and its bins. To keep the group, add it again as a whole (pnpm add -g eslint,prettier), or use pnpm update -g.
这两种形式可以混合使用。例如:
pnpm add -g eslint,prettier typescript
将 eslint 和 prettier 打包为一次孤立安装,同时单独安装 typescript。
目录布局#
{pnpmHomeDir}/global/v11/ 的内容如下所示:
{pnpmHomeDir}/global/v11/
├── {hash-A} → symlink → ./{hash-A-target}/
├── {hash-A-target}/ ← isolated install dir
│ ├── package.json ← lists the packages installed together
│ ├── pnpm-lock.yaml ← lockfile for this install group
│ └── node_modules/
│ ├── <pkg>/ ← top-level dep, symlinked into the global virtual store
│ └── .pnpm/ ← the group's install state
├── {hash-B} → symlink → ./{hash-B-target}/
└── {hash-B-target}/ ← another isolated install dir
{hash}条目是符号链接;pnpm 会扫描它们以枚举活动的安装。- The targets are real directories that act as ordinary pnpm projects. Each has its own
package.jsonand lockfile. - By default, the package contents live in the global virtual store at
{storeDir}/links, outside this directory. Each install group's direct dependencies (the entries at the root of itsnode_modules/) are symlinks into that store, so install groups that depend on the same package share one copy. WithvirtualStoreType: project, each install group keeps its packages in its ownnode_modules/.pnpm. - 二进制 shims 位于
{pnpmHomeDir}/bin/中,并指向相应的安装组的node_modules。
When a package is removed or its install group is replaced, the hash symlink is updated. Orphaned target directories older than five minutes are removed at the start of the next pnpm add -g, pnpm update -g, or pnpm self-update.
列出全局软件包#
pnpm list -g
pnpm list -g --json # 机器可读
pnpm list -g --parseable # 仅路径
Because each install group has its own lockfile, listing across multiple groups can only reliably aggregate the top-level packages they were installed with. Transitive dependency trees from different groups can't be coherently merged. 因此:
pnpm list -g(默认--depth=0)始终有效,并显示所有全局安装的软件包。pnpm list -g --depth=<n>(其中n > 0)仅在以下情况下显示完整的依赖关系树:- 只有一个全局安装组,或者
- 位置参数将请求范围缩小到单个安装组,例如
pnpm list -g eslint --depth=1。
如果请求 --depth>0 但无法将请求缩小到单个安装组,pnpm 将报错 ERR_PNPM_GLOBAL_LS_DEPTH_NOT_SUPPORTED。
管理全局软件包#
| 命令 | 描述 |
|---|---|
pnpm add -g <pkg> |
全局安装软件包 |
pnpm remove -g <pkg> |
移除一个全局安装的软件包(如果该软件包被打包在某个安装组中,则整个组将被移除) |
pnpm update -g [pkg] |
Update global packages (re-installs a package into a new isolated directory when its resolved dependency graph changed) |
pnpm list -g |
列出所有全局安装的软件包 |
pnpm outdated -g |
Check global packages for newer versions |
pnpm approve-builds -g |
Approve the build scripts of global packages |
注意
不再支持 pnpm install -g (无参数)。使用 pnpm add -g <pkg> 安装特定软件包。
These cannot be installed globally:
- pnpm itself.
pnpm add -g pnpmfails withERR_PNPM_GLOBAL_PNPM_INSTALL. Usepnpm self-update. - A package with a bin that a
pnpm shimalready provides. It fails withERR_PNPM_GLOBAL_BIN_CONFLICT. crate:and Python dependencies.
pnpm add -g rust installs a Rust toolchain, not the npm package named rust.
二进制文件位置#
全局安装的二进制文件存储在 PNPM_HOME 的 bin 子目录中(即 $PNPM_HOME/bin/)。 This keeps the PNPM_HOME directory clean. Internal directories like global/ and store/ don't pollute shell autocompletion when PNPM_HOME is on PATH.
Run pnpm setup to update your shell configuration so that $PNPM_HOME/bin is on your PATH.
你可以通过以下方式检查当前的全局 bin 目录:
pnpm bin -g
项目感知的全局二进制#
添加于:v12.0.0-rc.2(仅 pnpm v12)
全局命令可以跟随你当前所在的项目。当你在项目中运行全局命令,而该项目要求同一个工具的另一版本时,pnpm 会运行该项目所要求的版本。
最实用的场景是 Node.js 本身。假定某个项目固定了运行时版本:
{
"devEngines": {
"runtime": {
"name": "node",
"version": "^22.0.0",
"onFail": "download"
}
}
}
在该目录内执行裸 node 就会运行 Node.js 22,即便你全局的 Node.js 属于另一个主版本:
cd ~/projects/legacy-app
node --version # v22.x.x — 该版本由项目固定
cd ~
node --version # 你全局安装的版本
No shell hooks, .bashrc edits, or use-style commands are involved. The shims pnpm writes into the global bin directory do the dispatch themselves.
版本的选定方式#
pnpm 会自当前工作目录向上寻找最近的、提供该命令的项目,随后:
- For the runtimes (
node,deno,bun), only a runtime pin counts:devEngines.runtime, thenengines.runtime. Fornode, since v12.7.0, a.node-versionor.nvmrcfile also counts (see below). 固定的版本会按需下载至全局虚拟存储并直接执行。对于运行时,绝不会查询项目的node_modules/.bin,因此依赖不可能提供你运行的node。 - 对于**包管理器**(
npm、yarn、bun),该支持于 v12.0.0-rc.6 加入:以项目的packageManager或devEngines.packageManager固定版本为准,pnpm 会按需准备该版本。该锁定版本优先级高于全局安装的对应包管理器副本,因为它是项目自身指定的安装版本。 - 对于其他任何包,将使用项目的
node_modules/.bin/<name>。
pnpm 主目录内部的目录会被跳过,因为全局安装不属于项目。
If the project provides nothing, or the lookup is declined, the globally installed version runs. Commands never fail merely because dispatch did not apply.
Runtime download mirrors#
Project-aware global runtime commands download their pinned versions using the global configuration file and environment settings. They do not automatically load the project's pnpm-workspace.yaml. A project's mirror settings still apply to runtime dependencies installed by pnpm install.
An explicit PNPM_CONFIG_WORKSPACE_DIR or NPM_CONFIG_WORKSPACE_DIR environment setting selects a workspace whose pnpm-workspace.yaml can supply settings, including nodeDownloadMirrors. Lowercase spellings of these variables also work.
For example, to use a Node.js mirror for the global node command, put this in the global configuration file:
nodeDownloadMirrors:
release: https://npmmirror.com/mirrors/node/
Alternatively, configure tools.node.channels.release.
If pnpm install prints pnpm: failed to prepare node@runtime:..., check whether node --version fails the same way inside the project. A command launched through node can fail while preparing Node.js before the install starts. Check the mirror settings in the global configuration and any explicitly selected workspace, plus the PNPM_CONFIG_TOOLS and PNPM_CONFIG_NODE_DOWNLOAD_MIRRORS environment variables. See mirror-setting precedence.
Runtime preparation failures explain which configuration applies, including workspace-directory environment overrides. For Node.js, the diagnostic also lists the global mirror settings and environment variables to check.
.nvmrc and .node-version#
添加于:v12.7.0
The node shim also reads the Node.js version from a .node-version or .nvmrc
file, so projects set up for nvm, fnm, or n work without a devEngines.runtime
entry:
22
The nearest directory that declares a Node.js runtime in any of these ways
decides the version. Within one directory, package.json takes precedence
over .node-version, which takes precedence over .nvmrc.
Both files accept a version or range, optionally prefixed with v, as well as
node, stable, lts/*, and LTS codenames such as lts/jod. Comments are
ignored. A value that only nvm can act on, such as system, default, or a
custom alias, is ignored, and the directory counts as unpinned.
信任#
你 cd 进入的项目,并不会自动获准用它自己的二进制文件替代你全局的二进制文件。
本节描述默认的 auto 策略。在 auto 下:
-
稳定的 Node.js 发布会在运行前依据 Node.js 发布团队的签名进行验证,因此无需询问即可切换。(在基于 musl 的系统上,对应构建未签名,因此会归入需询问的情形。)
-
Everything else (Deno, Bun, Node.js prereleases, and any ordinary package you enable) asks once, per project and per candidate:
The project at "/home/user/projects/app" provides its own "tsc", which will be used instead of the globally installed one. Do you trust this project? [y/N] -
A package manager pin that resolves from the npm registry, such as
yarn@1ornpm, is verified against npm's signature, so it runs without asking. Bun and Yarn 6 release archives ask once, like everything else.
回答会保存在机器本地的注册表中,以项目目录以及该确切二进制文件的指纹作为键。若二进制文件或其提供者包发生变化,pnpm 会再次询问,而不会复用原来的批准。
在 CI 以及任何非交互式会话中,无法发起询问,因此会运行全局版本,且不会记录任何内容。
另外两个策略会影响哪些候选项会进入该询问。 prompt 会使每个候选项都经过该询问,包括已通过签名验证的稳定版 Node.js 发布;回答依旧会被记住,因此按项目与候选项各询问一次,而不是每次运行都询问。 always skips the question entirely and switches straight away, which is also what makes it usable in CI, where a prompt would otherwise fall back to the global version.
还有一道与策略无关的防护:项目的命令必须来自与全局命令相同的包。若某个项目提供的是来自其他包的仿冒 tsc,则不匹配,此时运行的是你的全局 tsc。
参与其中的包#
Runtimes, Rust toolchain shims, and globally installed package managers participate by default. The Rust toolchain shims (cargo, rustc, and the other Rust tools) that pnpm add -g rust or pnpm shim add rust creates follow the project's toolchain file. 自 v12.0.0-rc.6 起,全局安装包管理器(pnpm add -g yarn)也会为其添加一个条目,因此它会像全局安装的 Node.js 已经遵循 devEngines.runtime 那样遵循项目的固定版本。由你自行设置的条目(包括 false)会保持你设定的状态。
通过 globalShims 可启用其他包,键为提供命令的包名:
globalShims:
typescript: true
因为 pnpm 在安装时决定为哪些可执行文件写入调度填充程序,所以在启用某个已全局安装的包后,需要重新安装它:
pnpm add -g typescript
Turning a package off needs no reinstall. The setting is re-read on every dispatch. 同一设置可以按包禁用调度,或用 globalShims: false 整体禁用。若只针对单条命令,可设置 PNPM_SHIM_BYPASS=1:
PNPM_SHIM_BYPASS=1 node --version
A package that is not installed globally at all can still get a dispatching command, with pnpm shim add. That is what makes yarn work inside a Yarn project on a machine that has only pnpm.
注意
globalShims is deliberately not read from a project's pnpm-workspace.yaml. It is read only from the global configuration file, the pnpm home directory's own pnpm-workspace.yaml, and the environment. 具体细节见该设置的文档。
全局虚拟存储#
全局安装使用全局虚拟存储。软件包存储在 {storeDir}/links 中,并在全局安装之间共享。这样可以避免多个全局包依赖于相同库时进行冗余获取。
全局注册本地软件包#
要使本地软件包的二进制文件在系统范围内可用,请在软件包目录中使用 pnpm add -g . 命令:
cd ~/projects/my-tool
pnpm add -g .
这样就注册了软件包的 bin 条目,以便可以从任何地方调用它们。请参阅 pnpm link 了解更多详情。
配置#
Global packages use the global configuration. To change a setting only for global packages, set it under globalPackagesConfig in the global config.yaml.
构建脚本批准#
包含构建脚本(例如 postinstall)的全局软件包需要获得批准。当你安装需要运行构建脚本的全局软件包时,pnpm 会提示你以交互方式批准或拒绝构建。
你还可以使用 --allow-build 标志预先批准构建:
pnpm add -g --allow-build=esbuild esbuild
pnpm approve-builds --global
lets you decide after the fact instead. It collects the packages awaiting approval from every
install group, asks about them once, and writes the decisions to
globalPackagesConfig.permissions in the global config.yaml, then rebuilds only the
groups that contain something you approved:
pnpm approve-builds -g