pnpm CLI

短别名#

pn 是 pnpm 的简称别名,而 pnx 是 pnpm dlx 的简称别名。你可以在任何需要使用 pnpm 或 pnpx 的地方使用它们:

pn install
pn add express
pn build
pn test
pnx create-vue my-app

与 npm 的差别#

与 npm 不同,pnpm 会校验所有选项。例如, pnpm install --target_arch x64 将失败,因为 --target_arch 不是 pnpm install 的有效选项。

Some dependencies read an npm_config_ environment variable during their build. pnpm does not set these variables from its options, so set the variable yourself: npm_config_target_arch=x64 pnpm install.

Boolean flags#

A boolean flag can be written on its own, negated with a no- prefix, or given an explicit value:

pnpm install --prod            # devDependencies are skipped
pnpm install --no-prod         # devDependencies are installed
pnpm install --prod=false      # the same, since v12.4.0
pnpm install --prod=true       # the same as --prod

The --config. escape hatch takes a value the same way: --config.trust-lockfile=false.

Which project a command acts on#

Run from a subdirectory of a project, a command acts on the nearest ancestor directory that has a manifest, so pnpm bin from packages/app/src reports paths under packages/app. Two commands stay where you are: pnpm init creates its manifest in the current directory, and pnpm exec runs the command there.

配置项#

-C <path>, --dir <path>#

在 <path> 的目录中启动 pnpm,而不是当前的工作目录。

-w, --workspace-root#

运行起来就好像 pnpm 是在 工作空间 的根目录中启动的,而不是在当前工作目录中启动的。

--ignore-workspace#

Run as if the current project were standalone. pnpm does not discover a pnpm-workspace.yaml in a parent directory, so the workspace's settings and other projects do not apply to the command.

For example, this installs only the project in the current directory, even if it is located inside a workspace:

pnpm install --ignore-workspace

--workspace-packages <glob>#

Select the workspace's projects with a glob pattern, overriding the packages field of pnpm-workspace.yaml. Repeat the flag to add more patterns.

-y, --yes#

Automatically answer yes to prompts.

--env-file <path>#

Added in: v12.16.0

Load environment variables from a dotenv-style file before running. Repeat the flag to layer more files:

pnpm --env-file .env run build
pnpm --env-file .env --env-file .env.local install

Later files override variables defined in earlier files, and variables already in the environment are never overridden. Relative paths resolve against the working directory where pnpm was invoked. The loaded variables are visible to config resolution (PNPM_CONFIG_<KEY> overrides and ${VAR} tokens in user-level .npmrc files, so registry tokens can live in the env file instead of the shell), to lifecycle scripts, and to any pnpm the command dispatches to.

The files are read the way node --env-file reads them. Values are taken as written, and $VAR is not expanded. A leading UTF-8 byte-order mark is ignored. A missing file fails the command.

The flag is pnpm's own: a shim invocation forwards its arguments to the shim target untouched and never loads env files.

命令#

有关更多信息,请参阅各个 CLI 命令的文档。以下是简便的 npm 命令等效列表,可帮助你入门:

npm 命令 pnpm 等效
npm install pnpm install
npm i <pkg> pnpm add <pkg>
npm run <cmd> pnpm <cmd>
npx <pkg> pnx <pkg>

当使用未知命令时,pnpm 将搜索具有给定名称的脚本,因此 pnpm run lint 与 pnpm lint 相同。如果没有具有指定名称的脚本, 那么 pnpm 将以 shell 脚本的形式执行该命令,因此你可以执行类似 pnpm eslint 的操作(参见 pnpm exec)。

环境变量#

一些与 pnpm 无关的环境变量可能会改变 pnpm 的行为:

  • CI
  • NO_COLOR turns off colored output.

These environment variables configure pnpm itself:

  • pnpm_config_<setting> sets a setting. See Environment variables.
  • PNPM_HOME sets the pnpm home directory, which holds global packages, their bins, and the default store.
  • PNPM_SHIM_BYPASS=1 makes a global shim run its global target and ignore the project's pin. See project-aware global bins.

这些环境变量可能会影响 pnpm 将使用哪些目录来存储全局信息:

  • XDG_CACHE_HOME
  • XDG_CONFIG_HOME
  • XDG_DATA_HOME
  • XDG_STATE_HOME

You can search the docs to find the settings that use these environment variables.