安裝
必要條件
If you don't use the standalone script or @pnpm/exe to install pnpm, then you need to have Node.js (at least v22) to be installed on your system.
Looking for pnpm 12? It is currently in beta and installed differently from pnpm 11. See Installing the pnpm 12 beta.
使用指令稿來安裝
即使沒有安裝 Node.js,您還是可以透過下面提供的指令稿來安裝 pnpm。
Windows
使用 Powershell:
Invoke-WebRequest https://get.pnpm.io/install.ps1 -UseBasicParsing | Invoke-Expression
On Windows, Microsoft Defender can significantly slow down installation of packages. You can add pnpm to Microsoft Defender's list of excluded folders in a PowerShell window with administrator rights by executing:
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 -
The install script picks a glibc or musl build based on your system's libc, and a separate musl build is provided for Alpine and other musl-based distros. 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. If you see error while loading shared libraries: libatomic.so.1, install it with your distro's package manager:
- Debian/Ubuntu:
apt-get install -y libatomic1 - Fedora/RHEL:
dnf install -y libatomic
You may use the pnpm runtime command then to install Node.js.
在 Docker 容器中
# bash
wget -qO- https://get.pnpm.io/install.sh | ENV="$HOME/.bashrc" SHELL="$(which bash)" bash -
# sh
wget -qO- https://get.pnpm.io/install.sh | ENV="$HOME/.shrc" SHELL="$(which sh)" sh -
# dash
wget -qO- https://get.pnpm.io/install.sh | ENV="$HOME/.dashrc" SHELL="$(which dash)" dash -
安裝特定版本
在執行安裝指令稿之前,您可以設定 shell 變數 PNPM_VERSION 以安裝指定版本的 pnpm:
curl -fsSL https://get.pnpm.io/install.sh | env PNPM_VERSION=<version> sh -
使用 Corepack 來安裝
Due to an issue with outdated signatures in Corepack, Corepack should be updated to its latest version first:
npm install --global corepack@latest
自 v16.13 起,Node.js 推出了 Corepack——管理套件管理器的工具。 因為這是試驗性功能,需執行此命令以啟用 Corepack:
If you have installed Node.js with pnpm runtime Corepack won't be installed on your system, you will need to install it separately. 請參閱 #4029。
corepack enable pnpm
它會在您的電腦上自動安裝 pnpm,
您可以使用此命令來釘選專案中使用的 pnpm 版本
corepack use pnpm@latest-11
這會在專案的 package.json 中加入 "packageManager" 欄位,指示 Corepack 在進入專案時選用特定版本。 此功能對於需強調可重現性時很有用,因為所有使用 Corepack 的開發者都使用與您相同的版本。 當新版 pnpm 推出時,您可以再次執行上述命令。
Corepack cannot install pnpm 12 yet. It expects the pnpm package to contain a bin/pnpm.mjs file, which the native pnpm 12 package does not have. Use pnpm self-update, npm, or the standalone script to install the beta.
使用其他套件管理程式
使用 npm 來安裝
我們提供了兩個 pnpm CLI 包裝, pnpm 和 @pnpm/exe。 On pnpm 12 the two are identical, so there is no reason to prefer one over the other; the difference below applies to pnpm 11.
- 一般版本的
pnpm需要 Node.js 才能執行。 Since v11, pnpm is distributed as pure ESM. @pnpm/exe則與 Node.js 一起包成一個可執行檔,所以可以用於沒有安裝 Node.js 的系統上。 On Linux, glibc and musl builds are both provided and the right one is selected automatically; the glibc build requires glibc 2.27 or newer andlibatomic.so.1(see Linux runtime requirements for details). Not available for Intel macOS (darwin-x64) — installpnpminstead, see #11423.
npx pnpm@latest-11 dlx @pnpm/exe@latest-11 setup
或
npm install -g pnpm@latest-11
使用 Homebrew
如果您已安裝 Homebrew 套件管理器,可以使用下列命令來安裝 pnpm
brew install pnpm
使用 winget 來安裝
如果您已安裝 winget,可以使用此命令來安裝 pnpm
winget install -e --id pnpm.pnpm
使用 Scoop
如果您已安裝 Scoop,可以透過以下命令安裝 pnpm
scoop install nodejs-lts pnpm
使用 Choco 來安裝
如果您已安裝 Choco,可以使用此命令來安裝 pnpm
choco install pnpm
您想在 CI 伺服器上使用 pnpm 嗎? 請參閱持續整合 (Continuous Integration)。
Installing the pnpm 12 beta
pnpm 12 is a rewrite of pnpm in Rust and is currently in beta. It is not recommended for production use yet. Please report any issues you run into.
pnpm 12 has no intentional breaking changes compared to pnpm 11, so the rest of this documentation applies to both versions. Only installation differs while v12 is in beta: it is published under the next-12 tag on npm and as a prerelease on GitHub, so Homebrew, winget, Scoop and Chocolatey don't offer it yet.
Using pnpm
If you already have pnpm v11.10.0 or newer, this is the easiest way to switch:
pnpm self-update next-12
pnpm links the native binary directly, so nothing else is needed. Note that inside a project that pins pnpm through the packageManager field, self-update only updates that pin instead of installing pnpm globally.
Using npm
If you don't have pnpm installed yet:
npm install -g --allow-scripts=pnpm pnpm@next-12
--allow-scripts=pnpm is required on npm 11.16 and newer, which blocks install scripts by default. The published package is a small wrapper whose preinstall script replaces it with the native binary for your platform, so without the flag pnpm is left as a placeholder file that fails to run. Older versions of npm run install scripts anyway and ignore the flag, so the command above works on any version. For the same reason, don't install pnpm 12 with --ignore-scripts or --no-optional. If you install it with pnpm or Bun, allow the build scripts of the pnpm package.
Node.js 18 or newer is needed to run that install script, but not to run pnpm afterwards — pnpm 12 is a native binary.
Using a standalone script
Set PNPM_VERSION to the exact beta version (the POSIX script does not accept npm dist-tags).
On POSIX systems:
curl -fsSL https://get.pnpm.io/install.sh | env PNPM_VERSION=12.0.0-beta.2 sh -
On Windows, using PowerShell:
$env:PNPM_VERSION="12.0.0-beta.2"; Invoke-WebRequest https://get.pnpm.io/install.ps1 -UseBasicParsing | Invoke-Expression
This installs pnpm without requiring Node.js, and unlike pnpm 11 it also works on Intel macOS.
相容性
此表列出過去的 pnpm 與對應 Node.js 版本的相容性:
| Node.js | pnpm 8 | pnpm 9 | pnpm 10 | pnpm 11 | pnpm 12 |
|---|---|---|---|---|---|
| Node.js 14 | ❌ | ❌ | ❌ | ❌ | ❌ |
| Node.js 16 | ✔️ | ❌ | ❌ | ❌ | ❌ |
| Node.js 18 | ✔️ | ✔️ | ✔️ | ❌ | ✔️ |
| Node.js 20 | ✔️ | ✔️ | ✔️ | ❌ | ✔️ |
| Node.js 22 | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| Node.js 24 | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| Node.js 26 | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
pnpm 12 only needs Node.js when it is installed from npm; the version installed by the standalone script runs without Node.js.
疑難排解
如果 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 npm 來尋找 pnpm 存放的位置。 If you're on Windows, run 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 及其寫入磁碟的所有檔案,請參閱 [解除安裝 pnpm]。