跳至主要內容
版本: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 v16.14) to be installed on your system.

使用指令稿來安裝

即使沒有安裝 Node.js,您還是可以透過下面提供的指令稿來安裝 pnpm。

Windows

使用 Powershell:

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

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.

使用 Corepack 來安裝

Since v16.13, Node.js is shipping Corepack for managing package managers. 因為這是試驗性功能,需執行此命令以啟用 Corepack:

資訊

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 推出時,您可以再次執行上述命令。

使用 npm 來安裝

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

npm install -g @pnpm/exe
提示

您想在 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. 如果使用 Windows,請在 Git Bash 中執行前述命令。 您會得到 pnpm 命令的位置,例如:

$ 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 視窗,才能使設定生效。

解除安裝 pnpm

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