1. 一次性执行以下 PowerShell 命令
在你当前的 PowerShell 窗口中执行:
# 创建目录
New-Item -ItemType Directory -Force -Path `
"E:\cache\pnpm-store", `
"E:\cache\pnpm-global", `
"E:\cache\pnpm-bin", `
"E:\cache\npm-cache" | Out-Null
# pnpm 存储目录
pnpm config set store-dir "E:\cache\pnpm-store"
# pnpm 全局包目录
pnpm config set global-dir "E:\cache\pnpm-global"
# pnpm 全局命令目录
pnpm config set global-bin-dir "E:\cache\pnpm-bin"
# npm 缓存目录
npm config set cache "E:\cache\npm-cache"
# Node.js 包管理相关缓存
$env:PNPM_HOME = "E:\cache\pnpm-bin"
$env:npm_config_cache = "E:\cache\npm-cache"
2. 把环境变量永久设置到 E 盘
执行:
[Environment]::SetEnvironmentVariable(
"PNPM_HOME",
"E:\cache\pnpm-bin",
"User"
)
[Environment]::SetEnvironmentVariable(
"PNPM_STORE_DIR",
"E:\cache\pnpm-store",
"User"
)
然后将 E:\cache\pnpm-bin 添加到用户 PATH:
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
$binPath = "E:\cache\pnpm-bin"
if (($userPath -split ';') -notcontains $binPath) {
[Environment]::SetEnvironmentVariable(
"Path",
(($userPath.TrimEnd(';') + ";" + $binPath).TrimStart(';')),
"User"
)
}
注意: 如果之前通过 Corepack、npm 或独立安装程序安装了 pnpm,原来的 pnpm 启动器可能还在其他目录。不要直接删除旧文件,先确认路径。
3. 验证配置
关闭当前 PowerShell,重新打开后执行:
pnpm config get store-dir
pnpm config get global-dir
pnpm config get global-bin-dir
npm config get cache
where.exe pnpm
预期配置结果:
| 项目 | 目标位置 |
|---|---|
| pnpm Store | E:\cache\pnpm-store |
| 全局包 | E:\cache\pnpm-global |
| 全局命令 | E:\cache\pnpm-bin |
| npm 缓存 | E:\cache\npm-cache |
补充两点:
PNPM_STORE_DIR并不是所有 pnpm 版本都认可的配置方式,以pnpm config get store-dir的实际结果为准。where.exe pnpm显示的是 pnpm 启动器位置,不会因为设置 Store 就自动移动。若你希望 pnpm 本身也安装在 E 盘,还需要根据当前安装方式单独调整。
如果你愿意,可以再执行下面这条命令,把结果发给我,我可以进一步帮你检查是否还有目录留在 C 盘:
pnpm --version
where.exe pnpm
npm config get prefix