Skip to main content
Version: 9.x

pnpm add <pkg>

安装软件包及其依赖的任何软件包。默认情况下,任何新包都会作为生产依赖安装。

¥Installs a package and any packages that it depends on. By default, any new package is installed as a production dependency.

长话短说

¥TL;DR

命令意义
pnpm add sax保存到 dependencies
pnpm add -D sax保存到 devDependencies
pnpm add -O sax保存到 optionalDependencies
pnpm add -g sax 全局安装包
pnpm add sax@nextnext 标签安装
pnpm add sax@3.0.0指定版本 3.0.0

支持的包位置

¥Supported package locations

从 npm 注册表安装

¥Install from npm registry

默认情况下,pnpm add package-name 会从 npm 注册表 安装最新版本的 package-name

¥pnpm add package-name will install the latest version of package-name from the npm registry by default.

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

¥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:

  • tag:pnpm add express@nightly

  • version:pnpm add express@1.0.0

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

    ¥version range: pnpm add express@2 react@">=0.1.0 <0.2.0"

从工作区安装

¥Install from the workspace

请注意,当添加依赖并在 workspace 中工作时,将从配置的源安装软件包,具体取决于是否设置 link-workspace-packages 以及 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 link-workspace-packages is set, and use of the workspace: range protocol.

从本地文件系统安装

¥Install from local file system

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

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

  1. 来自 tarball 文件(.tar.tar.gz.tgz

    ¥from a tarball file (.tar, .tar.gz, or .tgz)

  2. 从目录

    ¥from a directory

示例:

¥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.

从远程 tarball 安装

¥Install from 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 存储库安装

¥Install from 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:

  • 来自默认分支的最新提交:

    ¥Latest commit from default branch:

pnpm add kevva/is-positive
  • Git 提交哈希:

    ¥Git commit hash:

pnpm add kevva/is-positive#97edff6f525f192a3f83cea1944765f769ae2678
  • Git 分支:

    ¥Git branch:

pnpm add kevva/is-positive#master
  • 相对于 refs 的 Git 分支:

    ¥Git branch relative to refs:

pnpm add zkochan/is-negative#heads/canary
  • Git 标签:

    ¥Git tag:

pnpm add zkochan/is-negative#2.0.1
  • V 前缀的 Git 标签:

    ¥V-prefixed Git tag:

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:

    ¥Strict semver:

pnpm add zkochan/is-negative#semver:1.0.0
  • V 前缀的严格 semver:

    ¥V-prefixed strict semver:

pnpm add andreineculau/npm-publish-git#semver:v0.0.7
  • Semver 版本范围:

    ¥Semver version range:

pnpm add kevva/is-positive#semver:^2.0.0
  • V 前缀的 semver 版本范围:

    ¥V-prefixed semver version range:

pnpm add andreineculau/npm-publish-git#semver:<=v0.0.7

从 Git 存储库的子目录安装

¥Install from a subdirectory of a Git repository

你也可以使用 path: 参数从 Git 托管的 monorepo 安装子目录。例如:

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

pnpm add RexSkz/test-git-subdir-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 提供商使用协议简写 [provier]:

¥You can use a protocol shorthand [provier]: 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 omited, it defaults to github:.

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

¥Install from a Git repository combining different parameters

可以通过用 & 分隔多个参数来组合它们。这对于 monorepos 的分支非常有用:

¥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.

选项

¥Options

--save-prod, -P

像常规 dependencies 一样安装指定的软件包。

¥Install the specified packages as regular dependencies.

--save-dev, -D

将指定的软件包安装为 devDependencies

¥Install the specified packages as devDependencies.

--save-optional, -O

将指定的软件包安装为 optionalDependencies

¥Install the specified packages as optionalDependencies.

--save-exact, -E

保存的依赖将使用精确的版本进行配置,而不是使用 pnpm 的默认 semver 范围运算符。

¥Saved dependencies will be configured with an exact version rather than using pnpm's default semver range operator.

--save-peer

使用 --save-peer 会将一个或多个包添加到 peerDependencies 并将它们安装为开发依赖。

¥Using --save-peer will add one or more packages to peerDependencies and install them as dev dependencies.

--ignore-workspace-root-check

除非使用 --ignore-workspace-root-check-w 标志,否则向根工作区包添加新依赖将失败。

¥Adding a new dependency to the root workspace package fails, unless the --ignore-workspace-root-check or -w flag is used.

例如,pnpm add debug -w

¥For instance, pnpm add debug -w.

--global, -g

全局安装包。

¥Install a package globally.

--workspace

仅当在工作区中找到新依赖时才添加它。

¥Only adds the new dependency if it is found in the workspace.

--filter <package_selector>

了解有关过滤的更多信息。

¥Read more about filtering.