Skip to main content
Version: Next

支持的软件包来源

pnpm 支持从各种来源安装软件包。这些来源分为两类:可信来源异常来源

🌐 pnpm supports installing packages from various sources. These sources are divided into two categories: trusted sources and exotic sources.

使用外来来源(如 Git 仓库或直接的 tarball URL)在被传递依赖使用时可能引入供应链风险。你可以通过将 blockExoticSubdeps 设置为 true 来防止传递依赖使用外来来源。

🌐 Exotic sources (like Git repositories or direct tarball URLs) can introduce supply chain risks when used by transitive dependencies. You can prevent transitive dependencies from using exotic sources by setting blockExoticSubdeps to true.

受信任的来源

🌐 Trusted sources

受信任的源对于直接依赖和传递依赖都被认为是安全的。

🌐 Trusted sources are considered safe for both direct and transitive dependencies.

npm 注册表

🌐 npm registry

pnpm add package-name 将默认从 npm 注册表 安装最新版本的 package-name

如果在工作区中执行,该命令将首先尝试检查工作区中的其他项目是否使用指定的包。如果是,则将安装已使用的版本范围。

🌐 If executed in a workspace, the command will first try to check whether other projects in the workspace use the specified package. If so, the already used version range will be installed.

你还可以通过以下方式安装软件包:

🌐 You may also install packages by:

  • 标签:pnpm add express@nightly
  • 版本:pnpm add express@1.0.0
  • 版本范围:pnpm add express@2 react@">=0.1.0 <0.2.0"

JSR 注册表

🌐 JSR registry

新增于:v10.9.0

🌐 Added in: v10.9.0

要从 JSR 注册表安装软件包,请使用 jsr: 协议前缀:

🌐 To install packages from the JSR registry, use the jsr: protocol prefix:

pnpm add jsr:@hono/hono
pnpm add jsr:@hono/hono@4
pnpm add jsr:@hono/hono@latest

这就像从 npm 安装一样,但会告诉 pnpm 通过 JSR 解析包。

🌐 This works just like installing from npm, but tells pnpm to resolve the package through JSR instead.

工作区

🌐 Workspace

请注意,在添加依赖并在 workspace 中工作时,包将根据是否设置了 linkWorkspacePackages 并使用 workspace: range protocol,从配置的源安装。

🌐 Note that 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.

本地文件系统

🌐 Local file system

从本地文件系统安装有两种方法:

🌐 There are two ways to install from the local file system:

  1. 从一个 tar 包文件(.tar.tar.gz.tgz
  2. 从目录

示例:

🌐 Examples:

pnpm add ./package.tar.gz
pnpm add ./some-directory

当你从一个目录安装时,会在当前项目的 node_modules 中创建一个符号链接,因此它与运行 pnpm link 是相同的。

🌐 When you install from a directory, a symlink will be created in the current project's node_modules, so it is the same as running pnpm link.

特殊来源

🌐 Exotic sources

非标准来源对于开发很有用,但如果存在传递依赖,则可能带来供应链风险。

🌐 Exotic sources are useful for development but may pose supply chain risks when used by transitive dependencies.

远程 tar 包

🌐 Remote tarball

该参数必须是以 "http://" 或 "https://" 开头的可获取 URL。

🌐 The argument must be a fetchable URL starting with "http://" or "https://".

示例:

🌐 Example:

pnpm add https://github.com/indexzero/forever/tarball/v0.5.6

Git 仓库

🌐 Git repository

pnpm add <git remote url>

从托管的 Git 提供程序安装包,并使用 Git 克隆它。

🌐 Installs the package from the hosted Git provider, cloning it with Git.

你可以通过以下方式从 Git 安装软件包:

🌐 You may install packages from Git by:

  • 来自默认分支的最新提交:
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

使用 semver 从 Git 存储库安装

🌐 Install from a Git repository using semver

你可以使用 semver: 参数指定要安装的版本(范围)。例如:

🌐 You can specify version (range) to install using the semver: parameter. For example:

  • 严格 semver:
pnpm add zkochan/is-negative#semver:1.0.0
  • V 前缀的严格 semver:
pnpm add andreineculau/npm-publish-git#semver:v0.0.7
  • Semver 版本范围:
pnpm add kevva/is-positive#semver:^2.0.0
  • V 前缀的 semver 版本范围:
pnpm add andreineculau/npm-publish-git#semver:<=v0.0.7

从 Git 存储库的子目录安装

🌐 Install from a subdirectory of a Git repository

你也可以使用 path: 参数仅从 Git 托管的单仓库中安装一个子目录。例如:

🌐 You may also install just a subdirectory from a Git-hosted monorepo using the path: parameter. For instance:

pnpm add RexSkz/test-git-subfolder-fetch#path:/packages/simple-react-app

通过完整 URL 从 Git 存储库安装

🌐 Install from a Git repository via a full URL

如果你想要更明确或使用其他 Git 托管,则可能需要拼出完整的 Git URL:

🌐 If you want to be more explicit or are using alternative Git hosting, you might want to spell out full 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 存储库安装

🌐 Install from a Git repository using hosting providers shorthand

对于某些 Git 提供商,你可以使用协议简写 [provider]:

🌐 You can use a protocol shorthand [provider]: for certain Git providers:

pnpm add github:zkochan/is-negative
pnpm add bitbucket:pnpmjs/git-resolver
pnpm add gitlab:pnpm/git-resolver

如果省略 [provider]:,它默认为 github:

🌐 If [provider]: is omitted, it defaults to github:.

结合不同参数从 Git 存储库安装

🌐 Install from a Git repository combining different parameters

可以通过使用 & 分隔符来组合多个参数。这对于单一代码库的分支来说非常有用:

🌐 It is possible to combine multiple parameters by separating them with &. This can be useful for forks of monorepos:

pnpm add RexSkz/test-git-subdir-fetch.git#beta\&path:/packages/simple-react-app

beta 分支安装,并且只安装 /packages/simple-react-app 目录下的子目录。

🌐 Installs from the beta branch and only the subdirectory at /packages/simple-react-app.