安装

前言#

如果你不使用独立脚本或 @pnpm/exe 来安装 pnpm,则您的系统需要安装 Node.js(至少 v22)。

使用独立脚本安装#

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

在 Windows 上#

警告

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

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

使用 PowerShell:

$env:PNPM_VERSION="latest-11"; Invoke-WebRequest https://get.pnpm.io/install.ps1 -UseBasicParsing | Invoke-Expression

On Windows, Microsoft Defender can slow down installation of packages. 你可以通过在具有管理员权限的 PowerShell 窗口中执行以下命令,将 pnpm 添加到 Microsoft Defender 的排除文件夹列表中:

Add-MpPreference -ExclusionPath $(pnpm store path)

在 POSIX 系统上#

Not supported on Intel macOS and arm64 musl Linux

The standalone script and @pnpm/exe do not run on Intel Macs (darwin-x64) or on arm64 musl Linux, such as Alpine on ARM. Install the pnpm package, which runs on your system Node.js, with npm install -g pnpm@latest-11 instead. On arm64 musl Linux you can also use pnpm 12, which has a native binary for it. See #11423 and #10443 for context.

curl -fsSL https://get.pnpm.io/install.sh | env PNPM_VERSION=latest-11 sh -

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

wget -qO- https://get.pnpm.io/install.sh | env PNPM_VERSION=latest-11 sh -

Linux 运行时要求

The install script picks a glibc or musl build based on your system's libc. A separate musl build is provided for Alpine and other musl-based distros on x64. The glibc build requires glibc 2.27 or newer plus libatomic.so.1. Both are present on most full distros but may be missing from minimal container images. 如果你看到 error while loading shared libraries: libatomic.so.1,请使用发行版的软件包管理器安装该库:

  • Debian/Ubuntu:apt-get install -y libatomic1
  • Fedora/RHEL:dnf install -y libatomic

提示

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

在 Docker 容器中#

# bash
wget -qO- https://get.pnpm.io/install.sh | env PNPM_VERSION=latest-11 ENV="$HOME/.bashrc" SHELL="$(which bash)" bash -
# sh
wget -qO- https://get.pnpm.io/install.sh | env PNPM_VERSION=latest-11 ENV="$HOME/.shrc" SHELL="$(which sh)" sh -
# dash
wget -qO- https://get.pnpm.io/install.sh | env PNPM_VERSION=latest-11 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 -

使用 npm#

npx get-pnpm latest-11

提示

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

兼容性#

以下是各版本 pnpm 与各版本 Node.js 之间的兼容性表格。

Node.js pnpm 8 pnpm 9 pnpm 10 pnpm 11
Node.js 14 ❌ ❌ ❌ ❌
Node.js 16 ✔️ ❌ ❌ ❌
Node.js 18 ✔️ ✔️ ✔️ ❌
Node.js 20 ✔️ ✔️ ✔️ ❌
Node.js 22 ✔️ ✔️ ✔️ ✔️
Node.js 24 ✔️ ✔️ ✔️ ✔️
Node.js 26 ✔️ ✔️ ✔️ ✔️

问题排查#

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

假设你在运行 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#

To update to the newest pnpm 11, run the self-update command with the major:

pnpm self-update 11

A bare pnpm self-update follows npm's latest tag, which now points at pnpm 12. See Installing pnpm v12 if that is what you want.

卸载 pnpm#

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