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 克隆它。你可以对某些 Git 提供商使用协议。例如,pnpm add github:user/repo

¥Installs the package from the hosted Git provider, cloning it with Git. You can use a protocol for certain Git providers. For example, pnpm add github:user/repo

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

¥You may install from Git by:

  • master 的最新提交:pnpm add kevva/is-positive

    ¥latest commit from master: pnpm add kevva/is-positive

  • commit:pnpm add kevva/is-positive#97edff6f525f192a3f83cea1944765f769ae2678

  • branch:pnpm add kevva/is-positive#master

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

    ¥version range: pnpm add kevva/is-positive#semver:^2.0.0

选项

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