pnpm shim <cmd>
新增于:v12.0.0-rc.6(仅限 pnpm v12)
管理项目感知型命令垫片 (shim) 。
由该命令创建的垫片 (shim) 背后没有全局安装。它的存在仅为了让当前项目决定具体运行哪个版本:pnpm shim add yarn 会创建一个 yarn 命令链接,该命令会运行当前项目所指定的版本,并按需提供该版本。在仅安装了 pnpm 的机器上,正是这一机制使得 yarn 能够在 Yarn 项目中正常运行。
创建垫片 (shim) 总是出于有意为之——无论是执行 pnpm setup 还是安装操作,都不会自动生成 shim,因为 shim 会覆盖(或“遮蔽”)PATH 环境变量中位于其之前的路径所解析到的内容。
命令#
add#
pnpm shim add <pkg>...
将每个包中各个可执行文件对应的垫片链接到全局二进制目录(pnpm bin -g),并将该包记录在 globalShims 设置中,以确保这些垫片能够正常分发执行。
pnpm shim add yarn
pnpm 提供的包管理器(npm、yarn、bun)无需查找:它们的可执行文件路径是预先已知的,包括相应的别名(如 yarn 与 yarnpkg,npm 与 npx)。对于其他任何包,可执行文件将从该包发布的清单中读取;若包未发布任何可执行文件,则会报错 ERR_PNPM_SHIM_NO_BINS。
如果某个可执行文件已存在于全局二进制目录中,说明它属于其他对象——例如全局安装的包或另一个包的垫片 ——因此 pnpm 会报错 ERR_PNPM_SHIM_BIN_CONFLICT,而不会覆盖或移除现有的可用命令。
在设置了 globalShims: false 的情况下尝试添加 shim 会导致 ERR_PNPM_SHIMS_DISABLED 错误:该设置会禁用所有与项目相关的 shim,因此即便添加了 shim,它在 PATH 中也无法发挥作用。
rm#
pnpm shim rm <pkg>...
别名: remove, uninstall
Removes the package's shims and the globalShims entry that add recorded for it.
ls#
pnpm shim ls
Alias: list
Lists every shim in the global bin directory with the policy governing it:
yarn (auto): yarn, yarnpkg
How a version is chosen#
A shim resolves the project you are standing in, walking up from the current working directory:
- For a package manager, the project's
packageManagerordevEngines.packageManagerpin decides the version, and pnpm provisions it. The pin outranks a copy of that package manager installed globally, because it is the project's own statement of what installs it. - For any other package, the project's
node_modules/.bin/<name>is used.
If nothing in the project provides the command, the globally installed version runs — a shim never makes a command fail merely because dispatch did not apply.
The trust rules of project-aware global bins apply here too: an npm-published package manager is verified against npm's signature for its exact version and switches without asking, while Bun and Yarn 6 arrive as checksum-pinned platform archives and go through the confirmation prompt. That answer is remembered per project and per binary, so moving the project's pin to another version asks again.