天天看點

node系列-使用nvm-windows管理nodejs版本在window上管理nodejs版本,安裝nvm-windows

在window上管理nodejs版本,安裝nvm-windows

第一次安裝使用nvm-windows,會遇到好多坑,請仔細按一下步驟安裝:

  • 在Windows下完全解除安裝已安裝的node.js

  • 從解除安裝程式解除安裝程式和功能,也可以直接右鍵node.js的安裝包并選擇解除安裝。
  • 重新啟動(或者重新啟動任務管理器殺死所有與節點相關的程序)。
  • 從下列的目錄中找到相關的内容并删除掉:
    • C:\Program Files (x86)\nodejs
    • C:\Program Files\nodejs
    • C:\Users{User}\AppData\Roaming\npm(或%appdata%\npm)
    • C:\Users{User}\AppData\Roaming\npm-cache(或%appdata%\npm-cache)
    • 檢查%PATH%環境變量以確定沒有引用Nodejs或npm存在。
  • 重新啟動電腦。
  • 下載下傳nvm-windows

位址:https://github.com/coreybutler/nvm-windows/releases

下載下傳最新版本安裝包即可:

node系列-使用nvm-windows管理nodejs版本在window上管理nodejs版本,安裝nvm-windows

解壓後裡面是nvm-setup.exe安裝檔案,預設安裝即可,記住預設安裝哦,不然安裝node時有可能沒有權限而無法建立node快捷方式。

  • 測試是否安裝成功(有坑)

打開CMD視窗,直行nvm,如果顯示版本等資訊,則說明安裝成功。

C:\Users\Administrator>nvm

Running version 1.1.8.

Usage:

  nvm arch                     : Show if node is running in 32 or 64 bit mode.
  nvm current                  : Display active version.
  nvm install <version> [arch] : The version can be a specific version, "latest" for the latest current version, or "lts" for the
                                 most recent LTS 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 ue the specified version. Optionally use "latest", "lts", or "newest".
                                 "newest" is the latest installed 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不是内部或外部指令,則需要做些檢查。

C:\Users\qdz>nvm
'nvm' 不是内部或外部指令,也不是可運作的程式
或批處理檔案。
           

第一步,檢查是否配置了path環境變量

node系列-使用nvm-windows管理nodejs版本在window上管理nodejs版本,安裝nvm-windows
node系列-使用nvm-windows管理nodejs版本在window上管理nodejs版本,安裝nvm-windows

第二步:如果上面環境變量都存在,則就要重新開機電腦,再次執行nvm則成功。

在使用之前一定要更換下載下傳源

如果不更換下載下傳源,會出現安裝失敗。也可能出現node安裝成功,npm安裝失敗問題。

下面示範的就是,因未更換下載下傳源,node安裝成功,但npm安裝失敗。

C:\Users\Administrator>nvm install 15.0.1
Downloading node.js version 15.0.1 (64-bit)...
Complete
Creating C:\Users\Administrator\AppData\Roaming\nvm\temp

Downloading npm version 7.0.3... Error while downloading https://github.com/npm/cli/archive/v7.0.3.zip - Get "https://github.com/npm/cli/archive/v7.0.3.zip": read tcp 192.168.10.224:58463->20.205.243.166:443: wsarecv: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x20 pc=0x2fa5a7]

goroutine 1 [running]:
nvm/web.Download(0x11810300, 0x2d, 0x11812400, 0x3e, 0x11bd7c10, 0x5, 0x0)
        C:/Users/corey/OneDrive/Documents/workspace/oss/coreybutler/nvm-windows/src/web/web.go:81 +0x1c7
nvm/web.GetNpm(0x11810481, 0x2a, 0x11bd7c10, 0x5, 0x119a4994)
        C:/Users/corey/OneDrive/Documents/workspace/oss/coreybutler/nvm-windows/src/web/web.go:209 +0x22e
main.install(0x1180a0c8, 0x6, 0x372ad3, 0x2)
        C:/Users/corey/OneDrive/Documents/workspace/oss/coreybutler/nvm-windows/src/nvm.go:295 +0x82c
main.main()
        C:/Users/corey/OneDrive/Documents/workspace/oss/coreybutler/nvm-windows/src/nvm.go:86 +0x88f
           

更換下載下傳源

nvm預設安裝目錄為:C:\Users\Administrator[目前windows使用者名]\AppData\Roaming\nvm

進入 nvm 安裝目錄,編輯 setting.txt,追加兩行代碼:

node_mirror: https://npm.taobao.org/mirrors/node/
npm_mirror: https://npm.taobao.org/mirrors/npm/
           

接下來我們開始使用nvm。

  • 檢視已安裝的nodejs版本

nvm list
           
  • 如果是第一次安裝,使用該指令結果如下:

C:\Users\Administrator>nvm list

No installations recognized.
           
  • 檢視可安裝的nodejs版本

nvm list available
           

輸入該指令結果如下:

|   CURRENT    |     LTS      |  OLD STABLE  | OLD UNSTABLE |
|--------------|--------------|--------------|--------------|
|    17.0.1    |   14.18.1    |   0.12.18    |   0.11.16    |
|    17.0.0    |   14.18.0    |   0.12.17    |   0.11.15    |
|   16.12.0    |   14.17.6    |   0.12.16    |   0.11.14    |
|   16.11.1    |   14.17.5    |   0.12.15    |   0.11.13    |
|   16.11.0    |   14.17.4    |   0.12.14    |   0.11.12    |
|   16.10.0    |   14.17.3    |   0.12.13    |   0.11.11    |
|    16.9.1    |   14.17.2    |   0.12.12    |   0.11.10    |
|    16.9.0    |   14.17.1    |   0.12.11    |    0.11.9    |
|    16.8.0    |   14.17.0    |   0.12.10    |    0.11.8    |
|    16.7.0    |   14.16.1    |    0.12.9    |    0.11.7    |
|    16.6.2    |   14.16.0    |    0.12.8    |    0.11.6    |
|    16.6.1    |   14.15.5    |    0.12.7    |    0.11.5    |
|    16.6.0    |   14.15.4    |    0.12.6    |    0.11.4    |
|    16.5.0    |   14.15.3    |    0.12.5    |    0.11.3    |
|    16.4.2    |   14.15.2    |    0.12.4    |    0.11.2    |
|    16.4.1    |   14.15.1    |    0.12.3    |    0.11.1    |
|    16.4.0    |   14.15.0    |    0.12.2    |    0.11.0    |
|    16.3.0    |   12.22.7    |    0.12.1    |    0.9.12    |
|    16.2.0    |   12.22.6    |    0.12.0    |    0.9.11    |
|    16.1.0    |   12.22.5    |   0.10.48    |    0.9.10    |

This is a partial list. For a complete list, visit https://nodejs.org/download/releases
           

這邊展示的版本并不全,我一般會直接去nodejs官網檢視可用版本,https://nodejs.org/en/download/releases/。

  • 安裝最新版本nodejs

nvm install latest
           
  • 安裝指定版本nodejs(必須是存在的版本号)

nvm install 17.0.0
           
  • 指定要使用的版本

nvm use 17.0.0
           
  • 解除安裝指定版本nodejs(正在使用的版本無法解除安裝)

nvm uninstall 17.0.0
           

上面就是nvm-windows的整個安裝過程了,如有疑問,請私信。

附錄:nvm 常用指令

nvm 提供了強大而簡潔的指令:

nvm arch [32|64]: 顯示 node.js 運作在 32位 還是 64位架構. 設定 32/64 可以覆寫預設的系統架構.
nvm install [arch]: 安裝特定版本. 版本可以填入具體的 node.js 版本号或“latest”表示最新的穩定版本.(可選)指定是安裝32位版本還是64位版本(預設為系統架構). 将[arch]設定為all以安裝32位和64位版本.
nvm list [available]: 列出所有已安裝的 node.js 版本. 鍵入 available 在結尾可以顯示目前可用于下載下傳的所有版本清單.
nvm on: 啟用 node.js 版本管理.
nvm off: 禁用 node.js 版本管理 (并不會解除安裝任何已安裝的版本).
nvm proxy [url]: 設定用于下載下傳的代理. 将[url]留白以檢視目前代理。将[url]設定為“none”以删除代理.
nvm uninstall: 解除安裝特定版本.
nvm use [arch]: 切換到指定的版本. 可選地指定32/64位體系結構.nvm use将繼續使用所選版本,但根據提供給``的值切換到32/64位模式。有關在特定目錄中使用“use”(或使用“.nvmrc”)的資訊,請參閱 issue #16.
nvm root: 指定一個存放 node.js 不同版本的根目錄. 如果為未設定任何值,則會顯示目前已設定的目錄.
nvm version: 顯示 nvm-windows 的目前版本.
nvm node_mirror: 設定 node 鏡像源. 中國使用者請使用 npm.taobao.org/mirrors/nod…
nvm npm_mirror: 設定 npm 鏡像源. 中國使用者請使用 npm.taobao.org/mirrors/npm…