1. 查看npm
安装源
npm config get registry
// https://registry.npm.taobao.org 显示这样就表示当前安装源是taobao镜像
2. 设置更改npm
安装源
// 将当前的npm安装源改为taobao镜像 https://registry.npmmirror.com/ 为淘宝镜像新域名
npm config set registry https://registry.npmmirror.com/
3. 使用nrm
切换安装源
由于各个地区不同的网络环境,以及镜像同步不全量的问题,在使用淘宝源时仍有可能遇到一次错误,使用 nrm 可以迅速在各个安装源之间进行切换,而且它还带有测速功能,这能让我们很方便地挑选出最适合自己使用的安装源。
- 安装
nrm
npm install -g nrm
- 查看当前以及所有可用的安装源
nrm ls
/*
npm ----- https://registry.npmjs.org/
cnpm ---- http://r.cnpmjs.org/
taobao -- https://registry.npm.taobao.org/
nj ------ https://registry.nodejitsu.com/
rednpm -- http://registry.mirror.cqupt.edu.cn
skimdb -- https://skimdb.npmjs.com/registry
*/
- 切换至相应的安装源
nrm use cnpm
// Registry has been set to: https://r.cnpmjs.org/
- 对各个安装源进行测速
nrm test
/*
npm ------ 755ms
yarn ----- 1109ms
tencent -- 227ms
cnpm ----- 1141ms
taobao --- 236ms
npmMirror - 1232ms
*/
4. 安装cnpm
如果node版本太高,cnpm会安装不成功,可以使用如下指定版本方式安装:
npm install cnpm@7.1.0 -g --registry=https://registry.npm.taobao.org
5. 使用nvm
管理node
版本
nvm 是 node 的版本管理工具。如果你使用的是 Linux/OS X 一类的操作系统的话,可能系统本身会自带 node ,但是系统自带的 node 版本往往会比较旧,在使用过程中可能会出现各种各样的问题。而手动在系统中安装两个以上版本的 node 是非常复杂和痛苦的。这种情况下就需要使用到 nvm。
- 安装
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.4/install.sh | bash
// 或者
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.4/install.sh | bash
- 查看可用的node版本
nvm is available
- 安装默认最新版本的
node
nvm install node
- 安装指定版本
nvm install 版本号 // nvm install 16.20.0
- 看系统中已经安装的 node 版本
nvm ls
- 切换至你想要使用的 node 版本
nvm use <node版本名称>
安装参考:
https://blog.csdn.net/m0_59910554/article/details/126308956
https://blog.csdn.net/weixin_46544600/article/details/128833601
6. 升级Node.js版本:
清理npm缓存:npm cache clean -f
安装版本管理工具:npm install -g n
升级到最新的版本:n latest(最新版本)n stable(最新稳定版本)