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@next | 从 next 标签安装 |
pnpm add sax@3.0.0 | 指定版本 3.0.0 |
支持的软件包来源
🌐 Supported package sources
pnpm 支持从各种来源安装软件包。有关详细文档,请参见 支持的软件包来源 页面,内容包括:
🌐 pnpm supports installing packages from various sources. See the Supported package sources page for detailed documentation on:
-
npm registry
-
JSR registry
-
Workspace packages
-
Local file system (tarballs and directories)
-
Remote tarballs
-
Git repositories (with semver, subdirectories, and more)
选项
🌐 Options
--save-prod, -P, -p
以普通 dependencies 安装指定的软件包。
🌐 Install the specified packages as regular dependencies.
--save-dev, -D, -d
将指定的软件包安装为 devDependencies。
🌐 Install the specified packages as devDependencies.
--save-optional, -O, -o
将指定的软件包安装为 optionalDependencies。
🌐 Install the specified packages as optionalDependencies.
--save-exact, -E, -e
已保存的依赖将配置为使用确切版本,而不是使用 pnpm 的默认语义化版本范围运算符。
🌐 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.
--save-catalog
新增于:v10.12.1
🌐 Added in: v10.12.1
将新依赖保存到默认的[目录]。
🌐 Save the new dependency to the default catalog.
--save-catalog-name <catalog_name>
新增于:v10.12.1
🌐 Added in: v10.12.1
将新依赖保存到指定的[目录]。
🌐 Save the new dependency to the specified catalog.
--config
新增于:v10.8.0
🌐 Added in: v10.8.0
将依赖保存到 configDependencies。
🌐 Save the dependency to configDependencies.
--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.
--allow-build
新增于:v10.4.0
🌐 Added in: v10.4.0
允许在安装期间运行安装后脚本的软件包名称列表。
🌐 A list of package names that are allowed to run postinstall scripts during installation.
示例:
🌐 Example:
pnpm --allow-build=esbuild add my-bundler
这将运行 esbuild 的安装后脚本,并将其添加到 pnpm-workspace.yaml 的 allowBuilds 字段中。因此,esbuild 将来总是被允许运行其脚本。
🌐 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.
--filter <package_selector>
--cpu=<name>
新增于:v10.14.0
🌐 Added in: v10.14.0
覆盖本地模块的 CPU 架构以进行安装。可接受的值与来自 process.arch 的 package.json 的 cpu 字段相同。
🌐 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
🌐 Added in: v10.14.0
覆盖本地模块的操作系统以进行安装。可接受的值与来自 process.platform 的 package.json 的 os 字段相同。
🌐 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
🌐 Added in: v10.14.0
覆盖本地模块的 libc 以进行安装。可接受的值与 package.json 的 libc 字段相同。
🌐 Override libc of native modules to install. Acceptable values are same as libc field of package.json.