支持的软件包来源
pnpm 支持从各种来源安装软件包。这些来源分为两类:可信来源 和 外来来源 。
使用外来来源(例如 Git 仓库或直接 tarball URL)时,传递依赖项可能会引入依赖链风险。你可以通过将 blockExoticSubdeps 设置为 true 来阻止传递依赖项使用外来来源。
可信来源#
可信来源对于直接依赖和传递依赖都被认为是安全的。
npm 源#
pnpm add package-name 默认会从 npm 源安装 package-name 的最新版本。
如果在工作空间中执行,该命令将首先去检查这个工作空间中的其他项目是否已经使用了这个指定的包。如果是,已经使用过的版本范围将被安装。
你还可以通过以下方式安装包:
- 标签:
pnpm add express@nightly - 版本:
pnpm addexpress@1.0.0 - 版本范围:
pnpm add express@2 react@">=0.1.0 <0.2.0"
JSR 源#
使用 jsr: 协议前缀从JSR 注册源安装:
pnpm add jsr:@hono/hono
pnpm add jsr:@hono/hono@4
pnpm add jsr:@hono/hono@latest
这和从 npm 安装的方式类似,只是告诉 pnpm 通过 JSR 解析包。
crates.io and PyPI#
添加于:v12.4.0(pnpm v12 专用)
A crate: or pypi: prefix asks for a package of another ecosystem, once that ecosystem is enabled in pnpm-workspace.yaml:
pnpm add crate:serde
pnpm add pypi:httpx
Such a dependency is recorded in that ecosystem's own manifest and lockfile, not in package.json or pnpm-lock.yaml. See Cargo dependencies and Python dependencies.
Provisioned tools#
Added in: v12.13.0 (pnpm v12 only)
The tool: protocol asks for a tool pnpm downloads from the tool's own releases instead of from npm: Node.js, Bun, Deno, and Yarn.
pnpm add node@tool:24
pnpm add bun@tool:1.3.0
pnx node@tool:22 --version
pnx yarn@tool:4 --version
yarn@tool:<version> picks Yarn from wherever that line is published, like yarn@<version> does.
runtime: is an alias of tool: and keeps working everywhere it worked before. pnpm-lock.yaml still records these entries with runtime:, such as node@runtime:24.6.0, so lockfiles stay readable by earlier pnpm versions.
命名注册源#
命名注册源 别名会针对特定注册源解析包,而不管默认注册源是什么:
pnpm add work:@corp/lib@^2.0.0
pnpm add gh:@my-org/private-pkg
pnpm add npmjs:left-pad
gh: (GitHub Packages) and npmjs: (the public npm registry) work without configuration. Any other alias must be mapped under namedRegistries in pnpm-workspace.yaml or in the global configuration file (config.yaml).
工作空间(Workspace)#
When adding dependencies and working within a workspace, packages
will be installed from the configured sources, depending on whether or not
linkWorkspacePackages is set, and use of the
workspace: range protocol.
本地文件系统#
有两种方法可以从本地文件系统安装:
- 从 tar 压缩包文件(
.tar、.tar.gz或.tgz) - 从目录
示例:
pnpm add ./package.tar.gz
pnpm add ./some-directory
pnpm add ./some-directory records the dependency with the link: protocol.
pnpm creates a symlink in the current project's node_modules, so it is the
same as running pnpm link.
To install a directory as a package instead, use the file: protocol, for
example pnpm add file:./some-directory. pnpm copies the directory into the
virtual store and installs its dependencies.
外来来源#
外来来源对开发很有用,但当被传递依赖关系使用时,可能会带来供应链风险。
远程 tarball#
pnpm sends registry credentials for tarball downloads over HTTPS or loopback HTTP. A redirect receives credentials only within the configured registry path on the same origin.
参数必须是一个可访问的 URL,以 "http://" 或 "https://" 开头。
示例:
pnpm add https://github.com/indexzero/forever/tarball/v0.5.6
Git 仓库#
pnpm add <git remote url>
从给定 URL 处的 Git 仓库安装该包。 Depending on the repository, pnpm either downloads a source archive from the Git host or clones the repository with Git. See how Git dependencies are resolved.
你可以通过以下方式从 Git 安装软件包:
- 来自默认分支的最新提交:
pnpm add kevva/is-positive
- Git 提交哈希:
pnpm add kevva/is-positive#97edff6f525f192a3f83cea1944765f769ae2678
- Git 分支:
pnpm add kevva/is-positive#master
- 相对于 refs 的 Git 分支:
pnpm add zkochan/is-negative#heads/canary
- Git 标签:
pnpm add zkochan/is-negative#2.0.1
- v 前缀的 Git 标签:
pnpm add andreineculau/npm-publish-git#v0.0.7
使用语义版本从 Git 存储库安装#
你可以使用 semver: 参数指定要安装的版本(范围)。例如:
- 严格的语义版本:
pnpm add zkochan/is-negative#semver:1.0.0
- v 前缀的严格语义版本:
pnpm add andreineculau/npm-publish-git#semver:v0.0.7
- 语义版本范围:
pnpm add kevva/is-positive#semver:^2.0.0
- v 前缀的语义版本范围:
pnpm add andreineculau/npm-publish-git#semver:<=v0.0.7
从 Git 存储库的子目录安装#
你也可以使用 path: 参数从 Git 托管的 monorepo 中仅安装子目录。例如:
pnpm add RexSkz/test-git-subfolder-fetch#path:/packages/simple-react-app
通过完整 URL 从 Git 存储库安装#
如果你想要更明确或使用其他 Git 托管,你可能需要拼出完整的 Git URL:
# git+ssh
pnpm add git+ssh://git@github.com:zkochan/is-negative.git#2.0.1
# https
pnpm add https://github.com/zkochan/is-negative.git#2.0.1
使用托管提供商简写从 Git 存储库安装#
你可以使用协议简写 [provider]: 来表示某些 Git 提供商:
pnpm add github:zkochan/is-negative
pnpm add bitbucket:pnpmjs/git-resolver
pnpm add gitlab:pnpm/git-resolver
pnpm add gist:106cfef49f8476b753a9cbbf9c65aff1
如果省略 [provider]:,则默认为 github:。
结合不同参数从 Git 存储库安装#
可以通过用 & 分隔来组合多个参数。这对于 monorepo 的分叉是有用的:
pnpm add RexSkz/test-git-subdir-fetch.git#beta\&path:/packages/simple-react-app
从beta 分支安装,并仅安装 /packages/simple-react-app 子目录。
pnpm also accepts npm's syntax, which separates the parameters with :::
pnpm add RexSkz/test-git-subdir-fetch.git#beta::path:packages/simple-react-app
Git 依赖如何解析#
添加于:v12.0.0(pnpm v12 专用)
For repositories on GitHub, GitLab, Bitbucket, and GitHub Gist, the specifier is an identity, not a choice of transport. 以下所有写法命名的都是同一个依赖,并且解析结果完全相同:
kevva/is-positive
github:kevva/is-positive
git+https://github.com/kevva/is-positive.git
https://github.com/kevva/is-positive.git
git+ssh://git@github.com/kevva/is-positive.git
每种写法都通过托管方的规范 HTTPS URL 解析,锁文件会记录以下两种形式之一:
- 托管方的源码归档,即普通的 tarball 下载。只有当针对该归档 URL 的匿名请求成功时才会记录这种形式,因此被记录的归档 URL 按其构造方式总是可获取的。
- 否则记录基于规范 HTTPS URL 的
git解析,任何能访问该仓库的机器都可以获取它。
pnpm 从不为这些托管方记录 SSH URL。某台机器使用哪种传输方式访问主机,属于该机器的 Git 配置,而非项目的属性。
为私有仓库使用 SSH#
在机器上的 Git 自身中配置改写:
git config --global url."git@github.com:".insteadOf https://github.com/
pnpm 会调用外部 git,因此该改写会自动作用于 pnpm 的所有 Git 操作。 The same holds on CI: give the runner an SSH key and this rewrite, or an HTTPS credential helper. The lockfile is identical either way.
其他主机上的仓库#
A URL that does not point at a known host (a self-hosted GitLab, Gitea, or any internal Git server) is kept exactly as written, transport included. For those, the URL is the identity. 内嵌凭据的 URL 同样会原样保留,并且绝不会解析为主机归档。
提示
Existing lockfile entries are left untouched. The rules above apply when an entry is added or re-resolved, and pnpm update <package> re-records an SSH URL over HTTPS.
Git 依赖如何构建#
新增于:v12.0.0-rc.6(仅限 pnpm v12)
A Git-hosted dependency that ships sources rather than a built package has to be prepared (its dependencies installed and its build script run) before it can be installed. pnpm prepares it with the package manager the dependency asks for. See Preparing a git-hosted dependency.