前言
对于nodejs的爱好者来说,必然希望能够在同一台机器上安装多个版本的nodejs(至少两个:稳定版和最新版)。稳定版用来在实际生产项目中使用,最新版本用来研究nodejs的新特性、踩坑。下面我将介绍一下在windows系统中如何通过nvm进行node多版本管理
nvm-windows的地址
nvm-windows的github地址:
https://github.com/coreybutler/nvm-windows
安装包下载地址:
https://github.com/coreybutler/nvm-windows/releases
安装及使用
- 安装包:
v1.1.0 setup版
安装环境:
Win10系统,64位
- 安装:
下载
nvm-windows解压安装,安装前首先要卸载已安装的任何版本的NodeJS,安装过程需要设置 NVM 的安装路径和NodeJS的快捷方式路径,可以选择任意路径(指定安装目录和当前所使用的nodejs的目录,这两个路径中不要带有特殊的字符以及空格,否则会在nvm use xxx的时候出错,无法正确解析指定的nodejs的版本的地址)。
在安装的时候,自动会把nvm和nodejs的目录添加到系统环境变量中(环境变量 NVM_HOME 和 NVM_SYMLINK),所以安装后可以直接测试安装是否成功。
- nvm命令
- 使用
nvm install 版本号命令安装指定版本的NodeJS
C:\>nvm install v10.16.2
Downloading node.js version 10.16.2 (64-bit)...
Complete
Creating C:\Dev\nvm\temp
Downloading npm version 6.9.0... Complete
Installing npm v6.9.0...
Installation complete. If you want to use this version, type
nvm use 10.16.2
- 安装成功后在
NVM安装目录下出现一个v10.16.2文件夹,使用nvm list命令查看已安装NodeJS列表
C:\>nvm list
10.16.2
- 再次使用 nvm install 版本号 命令安装另一版本的 NodeJS
C:\>nvm install v12.8.0
Downloading node.js version 12.8.0 (64-bit)...
Complete
Creating C:\Dev\nvm\temp
Downloading npm version 6.10.2... Complete
Installing npm v6.10.2...
Installation complete. If you want to use this version, type
nvm use 12.8.0
- 安装成功后在 NVM 安装目录下又多了一个 v12.8.0 目录,使用
nvm list命令查看已安装 NodeJS 列表,带 * 版本代表当前使用的 NodeJS 版本。
C:\>nvm list
12.8.0
* 10.16.2 (Currently using 64-bit executable)
- 使用 nvm use 版本号 切换需要使用的 NodeJS 版本,切换成功后可以使用 node -v 和 npm -v 命令查看是否切换成功。
C:\>nvm use v12.8.0
Now using node v12.8.0 (64-bit)
C:\>node -v
v12.8.0
C:\>npm -v
6.10.2
C:\>nvm use v10.16.2
Now using node v10.16.2 (64-bit)
C:\>node -v
v10.16.2
C:\>npm -v
6.9.0
备注
- NVM 安装成功后可以通过
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.
- 实际上不使用任何 NodeJS 版本管理工具也可以安装多个版本 NodeJS,方法:安装完一个版本后需要手动重命名安装目录,保证与环境变量路径不一致,然后再安装另一个版本,第二个版本安装好后再把第一个版本的安装目录命名恢复成安装时使用的名字,尽量保证低版本优先安装。这种方法的缺点在于切换 NodeJS 版本需要手动修改环境变量,使用 IDE 稍微好些因为可以手动配置 NodeJS 安装目录,不过在 IDE 中使用命令行工具仍然存在这样的问题。