Skip to main content
Version: Next

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>

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

You may install packages from Git by:

  • Latest commit from default branch:
pnpm add kevva/is-positive
  • Git commit hash:
pnpm add kevva/is-positive#97edff6f525f192a3f83cea1944765f769ae2678
  • Git branch:
pnpm add kevva/is-positive#master
  • Git branch relative to refs:
pnpm add zkochan/is-negative#heads/canary
  • Git tag:
pnpm add zkochan/is-negative#2.0.1
  • V-prefixed Git tag:
pnpm add andreineculau/npm-publish-git#v0.0.7

Install from a Git repository using semver

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

  • Strict semver:
pnpm add zkochan/is-negative#semver:1.0.0
  • V-prefixed strict semver:
pnpm add andreineculau/npm-publish-git#semver:v0.0.7
  • Semver version range:
pnpm add kevva/is-positive#semver:^2.0.0
  • V-prefixed semver version range:
pnpm add andreineculau/npm-publish-git#semver:<=v0.0.7

Install from a subdirectory of a Git repository

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

Install from a Git repository via a full 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

Install from a Git repository using hosting providers shorthand

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

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

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

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

Options

--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 的内容。