Skip to main content
Version: 10.x

安装

先决条件

🌐 Prerequisites

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

🌐 If you don't use the standalone script or @pnpm/exe to install pnpm, then you need to have Node.js (at least v18.12) to be installed on your system.

使用独立脚本

🌐 Using a standalone script

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

🌐 You may install pnpm even if you don't have Node.js installed, using the following scripts.

在 Windows 上

🌐 On Windows

warning

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

🌐 Sometimes, Windows Defender may block our executable if you install pnpm this way.

由于此问题,我们目前建议在 Windows 上使用 npmCorepack 安装 pnpm。

🌐 Due to this issue, we currently recommend installing pnpm using npm or Corepack on Windows.

使用 PowerShell:

🌐 Using PowerShell:

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

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

🌐 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 系统上

🌐 On POSIX systems

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

如果你没有安装 curl,你想使用 wget:

🌐 If you don't have curl installed, you would like to use wget:

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

然后你可以使用 pnpm env 命令来安装 Node.js。

🌐 You may use the pnpm env command then to install Node.js.

在 Docker 容器中

🌐 In a Docker container

# 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 -

安装特定版本

🌐 Installing a specific version

在运行安装脚本之前,你可以选择设置环境变量 PNPM_VERSION 来安装特定版本的 pnpm:

🌐 Prior to 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 -

使用 Corepack

🌐 Using Corepack

由于 Corepack 中的过期签名 问题,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 用于管理包管理器。这是一个实验性功能,因此你需要通过运行以下命令来启用它:

🌐 Since v16.13, Node.js is shipping Corepack for managing package managers. This is an experimental feature, so you need to enable it by running:

info

如果你使用 pnpm env 安装了 Node.js,Corepack 不会随系统安装,你需要单独安装它。详见 #4029

🌐 If you have installed Node.js with pnpm env Corepack won't be installed on your system, you will need to install it separately. See #4029.

corepack enable pnpm

这将自动在你的系统上安装 pnpm。

🌐 This will automatically install pnpm on your system.

你可以使用以下命令固定项目中使用的 pnpm 版本:

🌐 You can pin the version of pnpm used on your project using the following command:

corepack use pnpm@latest-10

这将在你的本地 package.json 中添加一个 "packageManager" 字段,用于指示 Corepack 在该项目中始终使用特定版本。如果你希望实现可复现性,这会非常有用,因为所有使用 Corepack 的开发者将会使用与你相同的版本。当 pnpm 发布新版本时,你可以重新运行上述命令。

🌐 This will add a "packageManager" field in your local package.json which will instruct Corepack to always use a specific version on that project. This can be useful if you want reproducability, as all developers who are using Corepack will use the same version as you. When a new version of pnpm is released, you can re-run the above command.

使用其他包管理器

🌐 Using other package managers

使用 npm

🌐 Using npm

我们提供两个 pnpm CLI 套装,pnpm@pnpm/exe

🌐 We provide two packages of pnpm CLI, pnpm and @pnpm/exe.

  • pnpm is an ordinary version of pnpm, which needs Node.js to run.

  • @pnpm/exe is packaged with Node.js into an executable, so it may be used on a system with no Node.js installed.

npx pnpm@latest-10 dlx @pnpm/exe@latest-10 setup

or

npm install -g pnpm@latest-10

使用 Homebrew

🌐 Using Homebrew

如果你安装了包管理器,则可以使用以下命令安装 pnpm:

🌐 If you have the package manager installed, you can install pnpm using the following command:

brew install pnpm

使用 winget

🌐 Using winget

如果你安装了 winget,则可以使用以下命令安装 pnpm:

🌐 If you have winget installed, you can install pnpm using the following command:

winget install -e --id pnpm.pnpm

使用 Scoop

🌐 Using Scoop

如果你安装了 Scoop,则可以使用以下命令安装 pnpm:

🌐 If you have Scoop installed, you can install pnpm using the following command:

scoop install nodejs-lts pnpm

使用 Choco

🌐 Using Choco

如果你安装了 Chocolatey,则可以使用以下命令安装 pnpm:

🌐 If you have Chocolatey installed, you can install pnpm using the following command:

choco install pnpm

使用 Volta

🌐 Using Volta

如果你安装了 Volta,则可以使用以下命令安装 pnpm:

🌐 If you have Volta installed, you can install pnpm using the following command:

volta install pnpm
tip

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

🌐 Do you wanna use pnpm on CI servers? See: Continuous Integration.

兼容性

🌐 Compatibility

以下是过去的 pnpm 版本以及相应 Node.js 版本支持的列表。

🌐 Here is a list of past pnpm versions with respective Node.js version support.

Node.jspnpm 8pnpm 9pnpm 10
Node.js 14
Node.js 16✔️
Node.js 18✔️✔️✔️
Node.js 20✔️✔️✔️
Node.js 22✔️✔️✔️
Node.js 24✔️✔️✔️

故障排除

🌐 Troubleshooting

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

🌐 If pnpm is broken and you cannot fix it by reinstalling, you might need to remove it manually from the PATH.

假设在运行 pnpm install 时你遇到了以下错误:

🌐 Let's assume you have the following error 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 命令的位置,例如:

🌐 First, try to find the location of pnpm by running: which pnpm. If you're on Windows, run where.exe pnpm.*. You'll get the location of the pnpm command, for instance:

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

既然你已经知道 pnpm CLI 的位置,打开该目录并删除所有与 pnpm 相关的文件(pnpm.cmdpnpx.cmdpnpm 等)。完成后,重新安装 pnpm,它应该可以正常工作。

🌐 Now that you know where the pnpm CLI is, open that directory and remove any pnpm-related files (pnpm.cmd, pnpx.cmd, pnpm, etc). Once done, install pnpm again and it should work as expected.

使用较短的别名

🌐 Using a shorter alias

pnpm 可能难以输入,所以你可以改用一个更短的别名,比如 pn

在 POSIX 系统上添加永久别名

🌐 Adding a permanent alias on POSIX systems

只需将以下行添加到你的 .bashrc.zshrcconfig.fish 中:

🌐 Just put the following line to your .bashrc, .zshrc, or config.fish:

alias pn=pnpm

在 Powershell (Windows) 中添加永久别名:

🌐 Adding a permanent alias in Powershell (Windows):

在具有管理员权限的 Powershell 窗口中,执行:

🌐 In a Powershell window with admin rights, execute:

notepad $profile.AllUsersAllHosts

在打开的 profile.ps1 文件中,输入:

🌐 In the profile.ps1 file that opens, put:

set-alias -name pn -value pnpm

保存文件并关闭窗口。你可能需要关闭所有打开的 PowerShell 窗口,以使别名生效。

🌐 Save the file and close the window. You may need to close any open Powershell window in order for the alias to take effect.

更新 pnpm

🌐 Updating pnpm

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

🌐 To update pnpm, run the self-update command:

pnpm self-update

卸载 pnpm

🌐 Uninstalling pnpm

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

🌐 If you need to remove the pnpm CLI from your system and any files it has written to your disk, see Uninstalling pnpm.