新旧项目需要不同版本的Node.js支持,比如同时使用vue2, vue3,这时就需要方便地切换Node.js的版本。本文将为您提供这样的便捷法门。
一、安装nvm
windows
下载解压到C:\greensoft\nvm-noinstall,在该目录下新建settings.txt,内容如下:
root: C:\greensoft\nvm-noinstall
path: C:\Program Files\nodejs
node_mirror: https://npm.taobao.org/mirrors/node/
npm_mirror: https://npm.taobao.org/mirrors/npm/
配置环境变量 NVM_HOME = C:\greensoft\nvm-noinstall NVM_SYMLINK = C:\Program Files\nodejs PATH添加%NVM_HOME%和 %NVM_SYMLINK%
注:1, node安装在NVM_HOME下,当前使用版本通NVM_SYMLINK链接。 2, 若以前安装过node, 应卸载并清除NODE相关环境变量,删除%appdata%\Roaming\npm-cache目录和%appdata%\local\npm-cache目录
linux
安装命令
sudo apt install curl
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
若出现错误:“Connection reset by peer in connection to raw.githubusercontent.com:443” 则
sudo apt install git
wget https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh
chmod a+x install.sh
./install.sh
安装完了,重启终端才能使用nvm。
二、使用nvm
- nvm list available查看可用版本
- nvm install <版本号> 安装node,如: nvm install 16.20.1
- 切换到vue目录, 如:cd \vue\smartlink-web
- nvm use <版本号>,如:nvm use 16.20.1
- 安装npm, 如:npm install -g npm@7.21.1
三、问题解决方法
1, npm install无响应问题
如:卡在sill idealTree buildDeps 原因:node的默认安装环境在国外,只需要修改下镜像的地址即可
npm config set registry https://registry.npm.taobao.org
npm cache clean --force