安装

前言#

pnpm 12 是一个原生可执行文件,安装后无需 Node.js。 The pnpm npm package installs on Node.js 18 or newer. The npx get-pnpm installer requires Node.js 22.13 or newer.

pnpm 12 是当前发布线。 The latest tag on npm points at it, so the commands below install pnpm 12 without naming a version.

支持的平台#

pnpm 12 为以下每个目标提供预构建的二进制文件:

操作系统 架构
Linux (glibc) x64, arm64, riscv64, ppc64le, s390x, loong64
Linux (musl) x64, arm64
macOS arm64, x64
Windows x64, arm64
FreeBSD x64
NetBSD x64
Android arm64, x64

FreeBSD、ppc64le、s390x、RISC-V 和 Android 已在 v12.4.0 中添加。 NetBSD 二进制文件是在 NetBSD 10 上构建和测试的。 On a target with no binary, install the JavaScript pnpm 11 instead.

For StackBlitz WebContainers, see the experimental WebAssembly distribution.

使用 pnpm#

如果你已经安装了 pnpm v11.10.0 或更新版本,可以直接更新到 pnpm 12:

pnpm self-update

Inside a project that pins pnpm through the packageManager field, self-update only updates that pin instead of installing pnpm globally.

使用独立脚本安装#

即使没有安装 Node.js,也可以使用以下脚本安装 pnpm。

在 Windows 上#

警告

如果你以这种方式安装 pnpm,Windows Defender 有时可能会阻止我们的可执行文件。

由于这个问题,我们目前建议在 Windows 上使用 npm 来安装 pnpm。

使用 PowerShell:

Invoke-WebRequest https://get.pnpm.io/install.ps1 -UseBasicParsing | Invoke-Expression

在 Windows 上,Microsoft Defender 可能会拖慢软件包的安装速度。你可以通过在具有管理员权限的 PowerShell 窗口中执行以下命令,将 pnpm 添加到 Microsoft Defender 的排除文件夹列表中:

Add-MpPreference -ExclusionPath $(pnpm store path)

在 POSIX 系统上#

curl -fsSL https://get.pnpm.io/install.sh | sh -

如果你没有安装 curl,也可以使用 wget:

wget -qO- https://get.pnpm.io/install.sh | sh -

提示

你可以使用 pnpm runtime 命令来安装 Node.js。

On Linux (kernel 5.9 or newer) with a LUKS2-encrypted NVMe SSD, dm-crypt queues reads and writes through kernel workqueues by default, which can slow down package installation. Find your crypt volume name with lsblk -o NAME,TYPE,MOUNTPOINTS, then bypass the workqueues and persist the setting in the LUKS2 header:

sudo cryptsetup refresh --perf-no_read_workqueue --perf-no_write_workqueue --persistent <crypt-volume-name>

在 Docker 容器中#

# bash
wget -qO- https://get.pnpm.io/install.sh | env ENV="$HOME/.bashrc" SHELL="$(which bash)" bash -
# sh
wget -qO- https://get.pnpm.io/install.sh | env ENV="$HOME/.shrc" SHELL="$(which sh)" sh -
# dash
wget -qO- https://get.pnpm.io/install.sh | env ENV="$HOME/.dashrc" SHELL="$(which dash)" dash -

安装特定版本#

Before running the install script, you may optionally set an env variable PNPM_VERSION to install a specific version of pnpm:

curl -fsSL https://get.pnpm.io/install.sh | env PNPM_VERSION=<version> sh -

Installing without changing PATH#

By default, the install scripts add pnpm to PATH through your shell configuration file, or on Windows, your user environment variables. To leave those unchanged, set PNPM_NO_MODIFY_PATH=1. pnpm is still installed in PNPM_HOME, and the installer prints the PNPM_HOME and PATH lines for you to add. This needs pnpm 12.16.0 or newer.

curl -fsSL https://get.pnpm.io/install.sh | env PNPM_NO_MODIFY_PATH=1 sh -
$env:PNPM_NO_MODIFY_PATH='1'; Invoke-WebRequest https://get.pnpm.io/install.ps1 -UseBasicParsing | Invoke-Expression

使用 npm#

npx get-pnpm

运行此安装程序需要 Node.js 22.13 或更新版本,但之后运行 pnpm 不需要。

To install without changing PATH, run npx get-pnpm --no-modify-path. See Installing without changing PATH.

You can also install the pnpm package globally with npm, which needs Node.js 18 or newer:

npm install -g pnpm@latest

提示

你想在 CI 服务器上使用 pnpm 吗?参见:持续集成。

兼容性#

以下是受支持的 pnpm 版本及其对应的 Node.js 版本支持列表。

Node.js pnpm 10 pnpm 11 pnpm 12
Node.js 18 ✔️ ❌ ✔️
Node.js 20 ✔️ ❌ ✔️
Node.js 22 ✔️ ✔️ ✔️
Node.js 24 ✔️ ✔️ ✔️
Node.js 26 ✔️ ✔️ ✔️

pnpm 12 只有在通过 npm 安装时才需要 Node.js,运行独立脚本来安装时无需 Node.js。

问题排查#

如果 pnpm 损坏并且你无法通过重新安装来修复,你可能需要将其从 PATH 中手动删除。

For example, a leftover install of an older, JavaScript-based pnpm earlier on PATH can shadow pnpm 12 and fail like this when running pnpm install:

C:\src>pnpm install
internal/modules/cjs/loader.js:883
  throw err;
  ^



Error: Cannot find module 'C:\Users\Bence\AppData\Roaming\npm\pnpm-global\4\node_modules\pnpm\bin\pnpm.js'
←[90m    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:725:27)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)←[39m
←[90m    at internal/main/run_main_module.js:17:47←[39m {
  code: ←[32m'MODULE_NOT_FOUND'←[39m,
  requireStack: []
}

首先,尝试通过运行 which pnpm 来找到 pnpm 的位置。如果你使用的是 Windows,请运行 where.exe pnpm.*。你将获得 pnpm 命令的位置,例如:

$ which pnpm
/c/Program Files/nodejs/pnpm

现在你已经知道了 pnpm CLI 的所在目录,打开它并删除所有与 pnpm 相关的文件(如 pnpm.cmd、 pnpx.cmd、 pnpm 等)。完成后,再次安装 pnpm。现在它应该按照预期正常工作。

更新 pnpm#

要更新 pnpm,请运行 self-update 命令:

pnpm self-update

卸载 pnpm#

如果你需要从系统中删除 pnpm CLI 以及它写入磁盘的任何文件,请参阅卸载 pnpm。