pnpm add <pkg>

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

TL;DR#

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

Supported package sources#

pnpm supports installing packages from various sources. See the Supported package sources page for detailed documentation on:

  • npm registry
  • JSR registry
  • 工作區套件
  • 本地檔案系統(tarball 與目錄)
  • 遠端的 tarballs
  • Git 倉庫(包含語義化版本、子目錄等)

Options#

--save-prod, -P, -p#

Install the specified packages as regular dependencies.

--save-dev, -D, -d#

Install the specified packages as devDependencies.

--save-optional, -O, -o#

Install the specified packages as optionalDependencies.

--save-exact, -E, -e#

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

--save-peer#

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

--save-catalog#

Added in: v10.12.1

Save the new dependency to the default catalog.

--save-catalog-name <catalog_name>#

Added in: v10.12.1

Save the new dependency to the specified catalog.

--config#

新增於 v10.8.0

Save the dependency to configDependencies.

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

Install a package globally. See Global Packages for details.

Each space-separated package is installed into its own isolated directory. To bundle several packages into a single isolated install (so they share dependencies and are removed together), pass them as a comma-separated list, e.g. pnpm add -g eslint,prettier.

--workspace#

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

--allow-build#

新增於 v10.4.0

A list of package names that are allowed to run postinstall scripts during installation.

例如:

pnpm --allow-build=esbuild add my-bundler

This will run esbuild's postinstall script and also add it to the allowBuilds field of pnpm-workspace.yaml. So, esbuild will always be allowed to run its scripts in the future.

Since v11.26.0, prefixing a name with ! denies the build instead:

pnpm add --allow-build=!core-js my-bundler

writes allowBuilds: { core-js: false }, so the package is never asked about again. Global installs (pnpm add -g) record the denial too.

--registry#

Specify the registry to use for this installation:

pnpm add --registry=https://registry.npmjs.org/ lodash

--filter <package_selector>#

Read more about filtering.

--cpu=<name>#

新增於 v10.14.0

Override CPU architecture of native modules to install. Acceptable values are same as cpu field of package.json, which comes from process.arch.

--os=<name>#

新增於 v10.14.0

Override OS of native modules to install. Acceptable values are same as os field of package.json, which comes from process.platform.

--libc=<name>#

新增於 v10.14.0

Override libc of native modules to install. Acceptable values are same as libc field of package.json.