nvm 下载和管理node版本
简介
nvm(Node Version Manager)是一款用于管理 Node.js 版本的工具,可以轻松地在一个系统中切换和安装多个 Node.js 版本。使用 nvm 可以让你轻松地升级或降级 Node.js 版本,也可以在同一台机器上同时使用多个版本的 Node.js。
nvm安装
0 卸载 Node
1.若之前已安装 Node,则需要先卸载
确保已删除 Node 的安装目录以及相关环境变量
2. 之后可以检查以下文件是否存在,若存在也删除:
(此处默认安装路径为C:\Program Files\nodejs)
C:\Program Files\nodejs
C:\Program Files (x86)\nodejs
C:\Users\{User}\AppData\Roaming\npm
C:\Users\{User}\AppData\Roaming\npm-cache
- cmd命令行检测系统中是否还存在nodejs
C:\>node -v
1 下载nvm
GitHub下载:https://github.com/coreybutler/nvm-windows/releases
- 下载exe能自动配置环境变量
2 配置nvm镜像源
- cmd命令行查看nvm的安装根路径
C:\>nvm root
直接复制路径去文件管理器地址栏粘贴地址进行访问,AppData文件夹可能为隐藏文件夹
- 找到setting.txt文件
在最后添加以下代码:
node_mirror: https://npm.taobao.org/mirrors/node/
npm_mirror: https://npm.taobao.org/mirrors/npm/
3 node不同版本安装及切换
- nvm安装成功后,可以通过cmd命令行的 nvm -v 命令查看所有可用的命令
C:\>nvm -v
Running version 1.1.7.
Usage:
nvm arch : Show if node is running in 32 or 64 bit mode.
nvm install <version> [arch] : The version can be a node.js version or "latest" for the latest stable version.
Optionally specify whether to install the 32 or 64 bit version (defaults to system arch).
Set [arch] to "all" to install 32 AND 64 bit versions.
Add --insecure to the end of this command to bypass SSL validation of the remote download server.
nvm list [available] : List the node.js installations. Type "available" at the end to see what can be installed. Aliased as ls.
nvm on : Enable node.js version management.
nvm off : Disable node.js version management.
nvm proxy [url] : Set a proxy to use for downloads. Leave [url] blank to see the current proxy.
Set [url] to "none" to remove the proxy.
nvm node_mirror [url] : Set the node mirror. Defaults to https://nodejs.org/dist/. Leave [url] blank to use default url.
nvm npm_mirror [url] : Set the npm mirror. Defaults to https://github.com/npm/cli/archive/. Leave [url] blank to default url.
nvm uninstall <version> : The version must be a specific version.
nvm use [version] [arch] : Switch to use the specified version. Optionally specify 32/64bit architecture.
nvm use <arch> will continue using the selected version, but switch to 32/64 bit mode.
nvm root [path] : Set the directory where nvm should store different versions of node.js.
If <path> is not set, the current root will be displayed.
nvm version : Displays the current running version of nvm for Windows. Aliased as v.
- 使用 nvm install <版本号>命令,安装指定版本的Node.js
C:\>nvm install v18.16.1
- 安装成功后在 NVM 安装目录下出现一个 v8.16.1 文件夹,使用 nvm list 命令查看已安装 NodeJS 列表
C:\Users\zhangsm>nvm list
18.16.1
- 再次安装并查看另一版本node
C:\>nvm install v17.9.1
C:\Users\zhangsm>nvm list
18.16.1
17.9.1
5. 使用nvm use <版本号> 切换需要使用的 Node.js 版本,切换成功后可以使用 node -v 和 npm -v 命令查看是否切换成功
C:\>nvm use v8.16.1
Now using node v8.16.1 (64-bit)
C:\>node -v
v8.16.1
C:\>npm -v
6.4.1
4 nvm常用命令
nvm off // 禁用node.js版本管理(不卸载任何东西)
nvm on // 启用node.js版本管理
nvm install <version> // 安装node.js的命名 version是版本号 例如:nvm install 8.12.0
nvm uninstall <version> // 卸载node.js是的命令,卸载指定版本的nodejs,当安装失败时卸载使用
nvm ls // 显示所有安装的node.js版本
nvm list available // 显示可以安装的所有node.js的版本
nvm use <version> // 切换到使用指定的nodejs版本
nvm v // 显示nvm版本
nvm install stable // 安装最新稳定版
5 常见问题
nvm切换版本失败、'node’不是内部或外部命令,也不是可运行的程序
可能情况:
- 环境变量配置存在问题,核对系统变量和用户变量是否一致
- 安装nvm前node没有卸载干净,需按照0-2检查删除