怎么在windows、linux、mac上安裝pnpm呢?
前言
如果您不使用獨立腳本或 @pnpm/exe 來安裝 pnpm,則需要在系統上安裝 Node.js(至少 v16.14)。
原址:https://pnpm.io/zh/installation
使用獨立腳本安裝
即使沒有安裝 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. 這是一項實驗性功能,因此您需要通過運行如下腳本來啟用它:
提示
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.js | pnpm 7 | pnpm 8 | pnpm 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:883throw 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
卸載pnpm可參考: Uninstalling pnpm.