安装
先决条件
¥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
使用 PowerShell:
¥Using PowerShell:
Invoke-WebRequest https://get.pnpm.io/install.ps1 -UseBasicParsing | Invoke-Expression
在 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 -
在 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
自 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:
corepack enable pnpm
如果你使用 Homebrew 安装 Node.js,则需要单独安装 corepack:
¥If you installed Node.js using Homebrew, you'll need to install corepack separately:
brew install corepack
这将自动在你的系统上安装 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
这将在本地 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
是 pnpm 的普通版本,需要 Node.js 才能运行。¥
pnpm
is an ordinary version of pnpm, which needs Node.js to run. -
@pnpm/exe
与 Node.js 一起打包成可执行文件,因此可以在未安装 Node.js 的系统上使用。¥
@pnpm/exe
is packaged with Node.js into an executable, so it may be used on a system with no Node.js installed.
npm install -g pnpm
或者
¥or
npm install -g @pnpm/exe
使用 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
你想在 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.js | pnpm 7 | pnpm 8 | pnpm 9 |
---|---|---|---|
Node.js 12 | ❌ | ❌ | ❌ |
Node.js 14 | ✔️ | ❌ | ❌ |
Node.js 16 | ✔️ | ✔️ | ❌ |
Node.js 18 | ✔️ | ✔️ | ✔️ |
Node.js 20 | ✔️ | ✔️ | ✔️ |
故障排除
¥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: []
}
首先,尝试通过运行以下命令查找 pnpm 的位置:which 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.cmd
、pnpx.cmd
、pnpm
等)。完成后,再次安装 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
。
¥pnpm
might be hard to type, so you may use a shorter alias like pn
instead.
在 POSIX 系统上添加永久别名
¥Adding a permanent alias on POSIX systems
只需将以下行添加到 .bashrc
、.zshrc
或 config.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.