npm和pnpm默认是安装到C盘的,久而久之就越堆越多

我们需要给他们在D盘建个文件夹

这里我的是

D:\npm-global

D:\npm-cache

D:\pnpm

D:\pnpm-cache

D:\pnpm-state

检测当前的情况

终端输入

powershell
123456789101112131415161718192021222324
Write-Host "===== NODE ====="
node -v
where.exe node

Write-Host "`n===== NPM ====="
npm -v
npm config get prefix
npm prefix -g
npm root -g
npm config get cache
where.exe npm
where.exe npx

Write-Host "`n===== PNPM ====="
pnpm -v
pnpm root -g
pnpm bin -g
pnpm store path
pnpm config get store-dir
pnpm config get cache-dir
pnpm config get state-dir
pnpm config get global-dir
pnpm config get global-bin-dir
where.exe pnpm

先把当前结果丢给ai,让它帮你看看这些东西在C盘的什么地方,然后再按照我如下的说明让ai帮你调整修改和删除旧配置

npm配置

npm是随着nodejs安装的

设置

powershell
12
npm config set prefix "D:\npm-global" --global
npm config set cache "D:\npm-cache" --global

验证

powershell
12
npm config get prefix
npm config get cache

我们使用npm安装pnpm

powershell
1
npm install --global pnpm

设置path变量

我们在powershell执行如下命令

powershell
123456789101112131415161718
$paths = @(
    "D:\npm-global",
    "D:\pnpm\bin"
)

$userPath = [Environment]::GetEnvironmentVariable("Path", "User") -split ";"

foreach ($path in $paths) {
    if ($userPath -notcontains $path) {
        $userPath += $path
    }
}

[Environment]::SetEnvironmentVariable(
    "Path",
    ($userPath -join ";"),
    "User"
)

或者手动添加

​ image-20261012102822201

这样之后我们使用npm安装的东西例如codex claude dsh hexo都会进入D:\npm-global

pnpm配置

推荐结构

  • store-dir D:\pnpm\store
  • cache-dir D:\pnpm-cache
  • state-dir D:\pnpm-state
  • global-dir D:\pnpm\global
  • global-bin-dir D:\pnpm\bin

设置path变量

powershell
1
[Environment]::SetEnvironmentVariable("PNPM_HOME", "D:\pnpm", "User")

​ image-20261012100720390

设置

powershell
12345
pnpm config set store-dir "D:\pnpm\store" --global
pnpm config set cache-dir "D:\pnpm-cache" --global
pnpm config set state-dir "D:\pnpm-state" --global
pnpm config set global-dir "D:\pnpm\global" --global
pnpm config set global-bin-dir "D:\pnpm\bin" --global

验证

powershell
123456
pnpm config get store-dir
pnpm config get cache-dir
pnpm config get state-dir
pnpm config get global-dir
pnpm config get global-bin-dir
pnpm store path