本文介紹 Windows 操作系統(tǒng)中安裝和管理多版本 NodeJS 的方法。
目錄
- NVM 簡(jiǎn)介
- 版本說(shuō)明
- 安裝步驟
- 備注
NVM 簡(jiǎn)介
NVM 全稱 Node Version Manager,是一個(gè)管理 NodeJS 版本的工具。
NVM 默認(rèn)只支持 Linux 和 OS X,不支持 Windows,針對(duì) Windows 操作系統(tǒng)有 2 個(gè)替代方案:
本文介紹使用 nvm-windows 安裝和管理多版本 NodeJS 的方法。
版本說(shuō)明
- nvm-windows
1.1.7
- NodeJS
10.16.2
&12.8.0
安裝步驟
- 下載 nvm-windows
- 解壓安裝,安裝前首先要卸載已安裝的任何版本的 NodeJS,安裝過(guò)程需要設(shè)置 NVM 的安裝路徑和 NodeJS 的快捷方式路徑,可以選擇任意路徑。
- NVM 安裝成功后會(huì)自動(dòng)生成環(huán)境變量
NVM_HOME
和NVM_SYMLINK
-
NVM_HOME
:NVM 安裝路徑 -
NVM_SYMLINK
: NodeJS 快捷方式路徑
-
- 使用
cmd
命令打開(kāi)命令提示符窗口,輸入nvm -v
校驗(yàn)是否安裝成功。
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.
- 進(jìn)入 NVM 安裝根目錄,查看
settings.txt
文件內(nèi)容
root: C:\Dev\nvm
path: C:\Dev\nodejs
-
root
:NVM 安裝路徑 -
path
:NodeJS 快捷方式路徑
- 在
settings.txt
文件中添加以下內(nèi)容
arch: 64
proxy:
node_mirror: http://npm.taobao.org/mirrors/node/
npm_mirror: https://npm.taobao.org/mirrors/npm/
-
arch
:Windows 操作系統(tǒng)位數(shù) -
proxy
:代理,淘寶 NodeJS 鏡像和 NPM 鏡像
- 使用
nvm install 版本號(hào)
命令安裝指定版本的 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 安裝目錄下出現(xiàn)一個(gè)
v10.16.2
文件夾,使用nvm list
命令查看已安裝 NodeJS 列表。
C:\>nvm list
10.16.2
- 再次使用
nvm install 版本號(hào)
命令安裝另一版本的 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 安裝目錄下又多了一個(gè)
v12.8.0
目錄,使用nvm list
命令查看已安裝 NodeJS 列表,帶*
版本代表當(dāng)前使用的 NodeJS 版本。
C:\>nvm list
12.8.0
* 10.16.2 (Currently using 64-bit executable)
- 使用
nvm use 版本號(hào)
切換需要使用的 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 安裝成功后可以通過(guò)
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.
- 實(shí)際上不使用任何 NodeJS 版本管理工具也可以安裝多個(gè)版本 NodeJS,方法:安裝完一個(gè)版本后需要手動(dòng)重命名安裝目錄,保證與環(huán)境變量路徑不一致,然后再安裝另一個(gè)版本,第二個(gè)版本安裝好后再把第一個(gè)版本的安裝目錄命名恢復(fù)成安裝時(shí)使用的名字,盡量保證低版本優(yōu)先安裝。這種方法的缺點(diǎn)在于切換 NodeJS 版本需要手動(dòng)修改環(huán)境變量,使用 IDE 稍微好些因?yàn)榭梢允謩?dòng)配置 NodeJS 安裝目錄,不過(guò)在 IDE 中使用命令行工具仍然存在這樣的問(wèn)題。