安装 npm/pnpm/yarn 换国内镜像 统一目录管理全局包+缓存


一、先统一目录(推荐放 D 盘)

Windows

cmd 复制代码
mkdir D:\web-pm\npm
mkdir D:\web-pm\pnpm
mkdir D:\web-pm\yarn

macOS / Linux

bash 复制代码
mkdir -p ~/web-pm/npm
mkdir -p ~/web-pm/pnpm
mkdir -p ~/web-pm/yarn

以后所有包管理器的全局包 + 缓存 都放在:
D:\web-pm 或 ~/web-pm


二、安装三大包管理器(npm 自带,只需装 pnpm + yarn)

1. npm(Node.js 自带,不用装)

安装 Node.js 自动带 npm

2. 安装 pnpm(推荐)

bash 复制代码
npm install -g pnpm

3. 安装 yarn(经典版)

bash 复制代码
npm install -g yarn

三、一键切换 国内镜像(淘宝镜像)

Windows

cmd 复制代码
npm config set registry https://registry.npmmirror.com
pnpm config set registry https://registry.npmmirror.com
yarn config set registry https://registry.npmmirror.com

macOS / Linux

bash 复制代码
npm config set registry https://registry.npmmirror.com
pnpm config set registry https://registry.npmmirror.com
yarn config set registry https://registry.npmmirror.com

四、统一修改:全局路径 + 缓存路径(最重要)

🔥 Windows 一键执行

cmd 复制代码
npm config set prefix D:\web-pm\npm\global
npm config set cache D:\web-pm\npm\cache

pnpm config set store-dir D:\web-pm\pnpm\store
pnpm config set global-dir D:\web-pm\pnpm\global
pnpm config set global-bin-dir D:\web-pm\pnpm\global\bin

yarn config set cache-folder D:\web-pm\yarn\cache
yarn config set global-folder D:\web-pm\yarn\global

🔥 macOS / Linux 一键执行

bash 复制代码
npm config set prefix ~/web-pm/npm/global
npm config set cache ~/web-pm/npm/cache

pnpm config set store-dir ~/web-pm/pnpm/store
pnpm config set global-dir ~/web-pm/pnpm/global
pnpm config set global-bin-dir ~/web-pm/pnpm/global/bin

yarn config set cache-folder ~/web-pm/yarn/cache
yarn config set global-folder ~/web-pm/yarn/global

五、配置环境变量(必须配置,否则全局命令用不了)

Windows

加到系统 PATH:

复制代码
D:\web-pm\npm\global
D:\web-pm\pnpm\global\bin
D:\web-pm\yarn\global\node_modules\.bin

macOS / Linux(zsh)

bash 复制代码
echo 'export PATH="$HOME/web-pm/npm/global/bin:$HOME/web-pm/pnpm/global/bin:$HOME/web-pm/yarn/global/node_modules/.bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

bash 用:

bash 复制代码
echo 'export PATH="$HOME/web-pm/npm/global/bin:$HOME/web-pm/pnpm/global/bin:$HOME/web-pm/yarn/global/node_modules/.bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

六、验证是否全部生效

bash 复制代码
npm config get registry
npm config get prefix
npm config get cache

pnpm config get registry
pnpm store path
pnpm config get global-dir

yarn config get registry
yarn cache dir
yarn global dir

✔ 最终统一目录结构

复制代码
web-pm/
├─ npm/
│  ├─ global   全局包
│  └─ cache    缓存
├─ pnpm/
│  ├─ global   全局包
│  └─ store    缓存仓库
└─ yarn/
   ├─ global   全局包
   └─ cache    缓存

相关推荐
verohh5 分钟前
从代码组织到功能抽象:AI 时代的前端工程架构设计
前端
颜进强6 分钟前
微信小微AI-评测|文档质量直接被判 0 分:小微团队评测那些没人告诉你的硬指标
前端·ai编程·全栈
Bacon8 分钟前
给项目做一次"全身 CT":Agent Skills 生态里的代码体检全链路
前端·人工智能·后端
颜进强11 分钟前
微信小微AI-提审与发布|评测过了不等于能上线:四道前置条件,缺一道都白提交
前端·ai编程·全栈
颜进强12 分钟前
微信小微AI-【实战二】从零写一个 skill|卡片死活不出来?这 4 个坑我们全踩了一遍
前端·ai编程·全栈
颜进强16 分钟前
微信小微AI-命中效果调优|在 AI 的「选择题」里,怎么让小微更愿意选中你(系列收官)
前端·ai编程·全栈
颜进强16 分钟前
微信小微AI-【实战四】给 Agent 立人设|一份 AGENTS.md,决定小微是靠谱助手还是满嘴跑火车
前端·ai编程·全栈
颜进强17 分钟前
微信小微AI - 【实战一】工程化落地|Taro 主工程里塞进一套原生 AI 代码,我们这样让它们共存
前端·ai编程·全栈
颜进强23 分钟前
微信小微AI-【实战三】接力落地|用户点完 AI 卡片跨进小程序,那几秒才是体验的生死线
前端·ai编程·全栈
费曼学习法24 分钟前
为什么你的 Subagent 拆完反而更慢?我把 12 万 token 的任务重做了一遍
前端