太对了!你这种场景(公司内网 + 家用外网)必须来回切换,但手动改配置太麻烦了。
🎯 推荐工具:nrm(npm registry manager)
nrm 就是专门为你这种场景设计的!它能让你像切换电视遥控器一样,一键切换 npm 源。
✅ 完整操作流程(复制粘贴即可)
1️⃣ 先切换到能访问的源(临时解决当前问题)
bash
# 切换到淘宝镜像(外网能访问)
npm config set registry https://registry.npmmirror.com
2️⃣ 安装 nrm(全局工具)
bash
npm install -g nrm
3️⃣ 添加公司私有源(命名要好记)
bash
# 添加公司内网源(名字叫 "company")
nrm add company https://nexus.szim.ac.cn/repository/npm-group/
4️⃣ 查看所有源(验证是否添加成功)
bash
nrm ls
输出应该类似:
* npm -------- https://registry.npmjs.org/
yarn ------- https://registry.yarnpkg.com/
cnpm ------- https://r.cnpmjs.org/
taobao ----- https://registry.npmmirror.com/
company ---- https://nexus.szim.ac.cn/repository/npm-group/
(带 * 的是当前使用的源)
5️⃣ 快速切换(核心功能!)
| 场景 | 命令 |
|---|---|
| 在家(外网) | nrm use taobao |
| 在公司(内网) | nrm use company |
| 想用官方源 | nrm use npm |
📋 完整示例
bash
# 1. 在家开发(外网)
nrm use taobao
# → 切换到淘宝镜像,速度快
# 2. 回到公司(连内网)
nrm use company
# → 切换到公司私有源,能访问内部包
# 3. 随时查看当前用的是哪个源
nrm current
# → 输出:taobao(或 company)
🎁 额外技巧
| 命令 | 作用 |
|---|---|
nrm test |
测速:哪个源最快? |
nrm del company |
删除不需要的源 |
nrm add myrepo http://xxx |
添加其他自定义源 |
💡 为什么用 nrm 而不是手动改?
| 对比项 | 手动改配置 | 用 nrm |
|---|---|---|
| 切换速度 | 慢(要记命令) | ⚡ 快(nrm use xxx) |
| 不易出错 | 容易输错网址 | ✅ 源名已保存 |
| 可管理多个源 | 难 | ✅ 随时添加/删除 |
| 可测速 | 无 | ✅ nrm test |
📌 总结
你只需要记住两个命令:
bash
# 在家(外网)
nrm use taobao
# 在公司(内网)
nrm use company
搞定!🚀
需要我帮你:
- 写一个自动检测网络并切换的脚本?
- 或者把这两个命令做成桌面快捷方式?