跳到主要内容

安装

Prerequisites

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

Using a standalone script

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

On Windows

Using PowerShell:

iwr https://get.pnpm.io/install.ps1 -useb | iex

On POSIX systems

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

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

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

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

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 -
提示

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

Using 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:

信息

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

If you installed Node.js using Homebrew, you'll need to install corepack separately:

brew install corepack

This will automatically install pnpm on your system.

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

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

通过 npm 安装 pnpm

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

  • pnpm is a 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

or

npm install -g @pnpm/exe

通过 Homebrew 安装 pnpm

如果你已经安装了 Homebrew 软件包管理器,则可以使用如下命令赖安装 pnpm:

brew install pnpm

Using winget

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

winget install pnpm

通过 Scoop 安装 pnpm

如果你已经安装了 Scoop,就可以通过一下命令安装 pnpm:

scoop install nodejs-lts pnpm

Using Choco

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

choco install pnpm

Using Volta

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

volta install pnpm
提示

你是否需要在 CI 服务器上使用 pnpm?请参阅 持续集成 章节。

兼容性

以下列表列出了以往的 pnpm 版本和对应支持的 Node.js 版本。

Node.jspnpm 5pnpm 6pnpm 7pnpm 8
Node.js 12✔️✔️
Node.js 14✔️✔️✔️
Node.js 16?️✔️✔️✔️
Node.js 18?️✔️✔️✔️
Node.js 20?️?✔️✔️

故障排查

如果 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 系统,请在 Git Bash 中运行此命令。 上述命令执行后,你将获得 pnpm 的安装位置,例如:

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

现在你知道 pnpm CLI 的安装位置了,请打开该位置的目录并删除所有于 pnpm 相关的文件(例如 pnpm.cmdpnpx.cmdpnpm 等)。 执行上述操作后,再重新安装 pnpm,应该就能用了。

Using a shorter alias

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

Adding a permanent alias on POSIX systems

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

alias pn=pnpm

Adding a permanent alias in Powershell (Windows):

In a Powershell window with admin rights, execute:

notepad $profile.AllUsersAllHosts

In the profile.ps1 file that opens, put:

set-alias -name pn -value pnpm

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

如果你需要从系统中删除 pnpm CLI 以及其写入到磁盘中的所有文件的话,请参阅 卸载 pnpm