跳到主要内容

pnpm add <pkg>

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

摘要

命令含义
pnpm add sax保存到 dependencies 配置项下
pnpm add -D sax保存到 devDependencies 配置项下
pnpm add -O sax保存到 optionalDependencies 配置项下
pnpm add -g sax 安装软件包到全局环境中
pnpm add sax@next安装标记为 next 的版本
pnpm add sax@3.0.0安装指定版本 3.0.0

支持的安装包地址

从 npm 注册表(npm registry)安装

pnpm add package-name 默认从 npm registry 安装最先版本的 package-name

如果是在 workspace 中执行的话,该命令将首先检查这个 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.

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

  • tag: pnpm add express@nightly
  • 具体版本:pnpm add express@1.0.0
  • 版本区间:pnpm add express@2 react@">=0.1.0 <0.2.0"

从 workspace 安装

需要注意的是,当使用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.

从本地文件系统上安装

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

  1. tar 压缩包(.tar.tar.gz.tgz
  2. 目录

示例:

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

当从目录安装时,会在当前项目 的 node_modules 目录下创建一个该目录的链接,就和执行 pnpm link 命令一样。

从远端的 tar 压缩包安装

URL 必须是一个以 "http://" 或 "https://" 开头的网络地址。

示例:

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

从 Git 仓库安装

pnpm add <git remote url>

利用 Git 从 Git 服务提供商处 clone 并安装软件包。 你可以使用特定 Git 服务提供商特定的地址协议。例如 pnpm add github:user/repo

你可以通过 Git 安装:

  • 来自 master 分支的最新 commit: pnpm add kevva/is-positive
  • commit: pnpm add kevva/is-positive#97edff6f525f192a3f83cea1944765f769ae2678
  • 分支: pnpm add kevva/is-positive#master
  • 版本区间: pnpm add kevva/is-positive#semver:^2.0.0

参数

--save-prod, -P

安装指定的软件包并添加到 dependencies 配置项中。

--save-dev, -D

安装指定的软件包并添加到 devDependencies 配置项中。

--save-optional, -O

安装指定的软件包并添加到 optionalDependencies 配置项中。

--save-exact, -E

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

--save-peer

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

--ignore-workspace-root-check

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

For instance, pnpm add debug -w.

--global, -g

将软件包安装都全局环境中。

--workspace

仅添加能在 workspace 中找到的依赖包。

--filter <package_selector>

阅读更多有关 filtering 的内容。