跳至主要內容
版本:9.x

pnpm add <pkg>

安裝一個套件與其所有依賴的套件。 預設狀況下,新安裝的套件將被視為生產環境依賴套件安裝。

TL;DR

命令效果
pnpm add saxSave to dependencies
pnpm add -D saxSave to devDependencies
pnpm add -O saxSave to optionalDependencies
pnpm add -g sax 全域安裝該套件
pnpm add sax@nextInstall from the next tag
pnpm add sax@3.0.0Specify version 3.0.0

支援的套件位置

從 npm registry 安裝

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

如果在工作區內執行此命令,此命令會先確認工作區中是否有其他專案也使用指定套件。 如果是,將會按已使用的版本範圍進行安裝。

您可以用下列方式安裝套件:

  • tag: pnpm add express@nightly
  • version: pnpm add express@1.0.0
  • version range: pnpm add express@2 react@">=0.1.0 <0.2.0"

從工作區安裝

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. from a tarball file (.tar, .tar.gz, or .tgz)
  2. 從一個資料夾

Examples:

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

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.

從遠端伺服器的 Tar 壓縮檔安裝

此參數必須是一個以 "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
  • Version range:
pnpm add kevva/is-positive#semver:^2.0.0

Semver

You can specify version (range) to install using the semver: parameter. 範例:

  • 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

Subdirectory

You may also install just a subdirectory from a Git-hosted monorepo using the path: parameter. 例如:

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

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

Providers shorthand

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

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

Parameters combination

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

# Install git branch `beta`
# Install only subfolder `/packages/simple-react-app`
pnpm add RexSkz/test-git-subfolder-fetch.git#beta&path:/packages/simple-react-app

Options

--save-prod, -P

Install the specified packages as regular dependencies.

--save-dev, -D

Install the specified packages as devDependencies.

--save-optional, -O

Install the specified packages as optionalDependencies.

--save-exact, -E

安裝套件並鎖定為指定版本,而不會使用 pnpm 預設的 semver 範圍

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

僅新增可在工作區找到的新套件。

--filter &lt;package_selector>

Read more about filtering.