Featured image of post 在 Windows 使用 nvm 安裝 Node 環境

在 Windows 使用 nvm 安裝 Node 環境

要如何在 Windows 使用 nvm 去管理開發的 Node.js 程式版本

在 Mac 開發 Node.js 軟體通常支援性是比較好的,但如果想在 Windows 開發 Node.js 的話,也可以用 NVM 來管理 Node.js 的版本,可以針對不同的專案需求去做簡單的 Node.js 版本切換

環境

Windows 10 x64 node.js 14.17.5

安裝 Windows 版本的 nvm-windows

到 GitHub coreybutler/nvm-windows 的 Repo 下載 nvm-setup.zip 檔案並進行安裝,檔案解壓所後照正常的安裝步驟安裝即可

我這邊使用的是 Windows PowerShell,如果安裝完成後必須要完整的重開 Windows PowerShell,這樣才能夠讓 Windows PowerShell 讀取到對的 NVM 環境變數

如果是要讓 VS Code 的 Terminal 能夠吃到 NVM 的環境變數,則需要整台電腦重開機才吃的到設定,只重開 VS Code 是沒有用的

NVM on Windows 10

列出可以安裝的 Node.js 版本

nvm list available

這邊指令跟 Mac 有點不一樣,不是使用 nvm ls-remote 去列出遠端可以安裝的 Node.js 版本,而是用 nvm list available

PS C:\Users\KJ> nvm list available

|   CURRENT    |     LTS      |  OLD STABLE  | OLD UNSTABLE |
|--------------|--------------|--------------|--------------|
|    16.7.0    |   14.17.5    |   0.12.18    |   0.11.16    |
|    16.6.2    |   14.17.4    |   0.12.17    |   0.11.15    |
|    16.6.1    |   14.17.3    |   0.12.16    |   0.11.14    |
|    16.6.0    |   14.17.2    |   0.12.15    |   0.11.13    |
|    16.5.0    |   14.17.1    |   0.12.14    |   0.11.12    |
|    16.4.1    |   14.16.1    |   0.12.12    |   0.11.10    |
|    16.4.0    |   14.16.0    |   0.12.11    |    0.11.9    |
|    16.3.0    |   14.15.5    |   0.12.10    |    0.11.8    |
|    16.2.0    |   14.15.4    |    0.12.9    |    0.11.7    |
|    16.1.0    |   14.15.3    |    0.12.8    |    0.11.6    |
|    16.0.0    |   14.15.2    |    0.12.7    |    0.11.5    |
|   15.14.0    |   14.15.1    |    0.12.6    |    0.11.4    |
|   15.13.0    |   14.15.0    |    0.12.5    |    0.11.3    |
|   15.12.0    |   12.22.5    |    0.12.4    |    0.11.2    |
|   15.11.0    |   12.22.4    |    0.12.3    |    0.11.1    |
|    15.9.0    |   12.22.2    |    0.12.1    |    0.9.12    |
|    15.8.0    |   12.22.1    |    0.12.0    |    0.9.11    |

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

如果沒有看到自己想要安裝的版本,可以到 Node.js Release 去找自己需要的 Node.js 版本

安裝 Node.js 14.17.5

nvm install

使用 nvm install 去安裝 Node.js 後會出現作業系統版本不符合,我系統是使用 Windows 10 64 bits 的系統,預設安裝的版本是 32 bits 的版本

PS C:\Users\KJ> nvm install 14.17.5
nvm.0.0
Node.js vnvm.0.0 is only available in 32-bit.

要使用 64 bits 版本時,在指令最後方加入 64 的字樣,就可以指定 64 bits 的版本

nvm install 14.17.5 64

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

Downloading npm version 6.14.14... Complete
Installing npm v6.14.14...

Installation complete. If you want to use this version, type

nvm use 14.17.5

指定使用 Node.js 版本

nvm use 14.17.5

安裝完成後可以使用 nvm use 去指定要使用的 Node.js 版本,在過程中會要求管理者權限去做處理

NVM 會在環境變數設定 2 個變數 NVM_HOMENVM_SYMLINK,在使用 nvm use 時 NVM 會將指定的 Node.js 版本關連到 NVM_SYMLINK 路徑下,所以需要管理者權限去做關聯存取

名稱 設定 說明
NVM_HOME C:\Users\KJ\AppData\Roaming\nvm NVM 存放目錄
NVM_SYMLINK C:\Program Files\nodejs NVM 軟連結路徑

這樣我們就可以開始在 Windows 10 使用 NVM 去管理 Node.js 版本了

列出已安裝的 Node.js 版本

nvm list

PS C:\Users\KJ>nvm list

    16.7.0
  * 14.17.5 (Currently using 64-bit executable)

nvm 指令清單

nvm     
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.

參考資料

comments powered by Disqus
All rights reserved,未經允許不得隨意轉載
Built with Hugo
Theme Stack designed by Jimmy