跳到主内容
版本:9.x

安装

前言

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.

使用独立脚本安装

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

Windows

warning

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

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

使用 PowerShell:

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

在 POSIX 系统上

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

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

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

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

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 -

安装特定版本

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 安装

Since v16.13, Node.js is shipping Corepack for managing package managers. 这是一项实验性功能,因此您需要通过运行如下脚本来启用它:

提示

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

如果您已经使用Homebrew安装了Node.js,您需要单独安装Corepack:

brew install corepack

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

你可以通过下列命令固定项目所用的 pnpm 版本:

corepack use pnpm@latest

This will add a "packageManager" field in your local package.json which will instruct Corepack to always use a specific version on that project. 如果您想要可复现性,这可能很有用,因为所有使用 Corepack 的开发人员都将使用与您相同的版本。 当一个新版本的 pnpm 发布时,您可以重新运行上述命令。

Using other package managers

使用 npm 安装

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.
npm install -g pnpm

或者

npm install -g @pnpm/exe

使用 HomeBrew 安装

如果您已经安装了这个包管理器,您可以使用下面的命令安装pnpm:

brew install pnpm

使用winget安装

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

winget install -e --id pnpm.pnpm

使用 Scoop 安装

如果您已经安装了Scoop,您可以使用下面的指令安装pnpm:

scoop install nodejs-lts pnpm

使用 Choco安装

如果您已经安装了Chocolatey,您可以使用下面的指令安装pnpm:

choco install pnpm

使用 Volta

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

volta install pnpm
提示

是否要在 CI 服务器上使用 pnpm? See: Continuous Integration.

兼容性

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

Node.jspnpm 7pnpm 8pnpm 9
Node.js 12
Node.js 14✔️
Node.js 16✔️✔️
Node.js 18✔️✔️✔️
Node.js 20✔️✔️✔️

问题排查

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

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: []
}

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

Now that you know where the pnpm CLI is, open that directory and remove any pnpm-related files (pnpm.cmd, pnpx.cmd, pnpm, etc). 完成后,再次安装 pnpm。现在它应该按照预期正常工作。

使用更短的别名

pnpm might be hard to type, so you may use a shorter alias like pn instead.

在 POSIX 系统上添加永久别名

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

alias pn=pnpm

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

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

notepad $profile.AllUsersAllHosts

In the profile.ps1 file that opens, put:

set-alias -name pn -value pnpm

保存文件并关闭窗口。 您可能需要关闭所有打开的 Powershell 窗口才能使别名生效。

Updating pnpm

To update pnpm, run the self-update command:

pnpm self-update

卸载 pnpm

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