Windows 下多版本 NodeJS 安裝和管理

本文介紹 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

安裝步驟

  1. 下載 nvm-windows
  2. 解壓安裝,安裝前首先要卸載已安裝的任何版本的 NodeJS,安裝過(guò)程需要設(shè)置 NVM 的安裝路徑和 NodeJS 的快捷方式路徑,可以選擇任意路徑。
  3. NVM 安裝成功后會(huì)自動(dòng)生成環(huán)境變量 NVM_HOMENVM_SYMLINK
    • NVM_HOME :NVM 安裝路徑
    • NVM_SYMLINK : NodeJS 快捷方式路徑
  4. 使用 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.
  1. 進(jìn)入 NVM 安裝根目錄,查看 settings.txt 文件內(nèi)容
root: C:\Dev\nvm
path: C:\Dev\nodejs
  • root:NVM 安裝路徑
  • path:NodeJS 快捷方式路徑
  1. settings.txt 文件中添加以下內(nèi)容
arch: 64
proxy:
node_mirror: http://npm.taobao.org/mirrors/node/
npm_mirror: https://npm.taobao.org/mirrors/npm/
  1. 使用 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
  1. 安裝成功后在 NVM 安裝目錄下出現(xiàn)一個(gè) v10.16.2 文件夾,使用 nvm list 命令查看已安裝 NodeJS 列表。
C:\>nvm list

    10.16.2
  1. 再次使用 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
  1. 安裝成功后在 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)
  1. 使用 nvm use 版本號(hào) 切換需要使用的 NodeJS 版本,切換成功后可以使用 node -vnpm -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)題。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。