1. 程式人生 > 實用技巧 >Node 多版本管理:NVM

Node 多版本管理:NVM

安裝前清理原有的 node,環境變數,快取(e.g. "C:\Users\<user>\AppData\Roaming\npm*")

儲存配置(e.g. C:\Users\<user>\.npmrc;C:\Users\<user>\AppData\Roaming\npm\etc\.npmrc)

下載(有安裝版與解壓版,以解壓為例)

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

解壓後執行管理員方式執行install.cmd,輸入 MVN 解壓的絕對路徑即可。

指令碼主要就是設定了環境變數,也可自己設定。設定完後會在 shell 的所在路徑生成一個settings.txt,記錄路徑。

NVM_HOME    D:\PcAPP\MVM
NVM_SYMLINK    C:\Program Files\nodejs
Path    %NVM_HOME%;%NVM_SYMLINK%

settings.txt

root: D:\PcAPP\MVM
arch: 64
proxy: none
originalpath: .
originalversion: 
node_mirror: https://npm.taobao.org/mirrors/node/
npm_mirror: https://npm.taobao.org/mirrors/npm/

不設定的話可能會報如下錯誤

ERROR open \settings.txt: The system cannot find the file specified.

然後就可以使用了,在使用之前最好設定一下映象源

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

常用命令

NVM for Windows is a command line tool. Simply typenvmin the console for help. The basic commands are:

  • nvm arch [32|64]: Show if node is running in 32 or 64 bit mode. Specify 32 or 64 to override the default architecture.
  • 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.
  • nvm list [available]: List the node.js installations. Typeavailableat the end to show a list of versions available for download.
  • nvm on: Enable node.js version management.
  • nvm off: Disable node.js version management (does not uninstall anything).
  • 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 uninstall <version>: Uninstall 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 based on the value supplied to<arch>. For information about usingusein a specific directory (or using.nvmrc), please refer toissue #16.
  • 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.
  • nvm node_mirror <node_mirror_url>: Set the node mirror.People in China can usehttps://npm.taobao.org/mirrors/node/
  • nvm npm_mirror <npm_mirror_url>: Set the npm mirror.People in China can usehttps://npm.taobao.org/mirrors/npm/
# 顯示已安裝 node 版本
nvm ls

# 顯示線上所有版本
 nvm ls available

# 顯示 nvm 版本
nvm

# 安裝指定版本
nvm install 14.6.0

# 安裝最新穩定版
nvm install latest

# 使用指定版本(切換版本後,全域性安裝的需要重新安裝)
nvm use 14.6.0

配置 npm 全域性安裝路徑

npm config set prefix "D:\nodejs\npm-global"

在 C:\Users\使用者名稱\ 下會生成 .npmrc 檔案,可以看到:prefix=D:\nodejs\npm-global


https://github.com/coreybutler/nvm-windows