Node.js的多版本管理工具之nvm

1,519 阅读5分钟

我报名参加金石计划1期挑战——瓜分10万奖池,这是我的第2篇文章,点击查看活动详情

nvm

nvm是Node.js版本管理的实用程序,通常有些项目必须依赖不同版的 NodeJS 运行环境,此时nvm应运而生,用于管理node的版本以及能很方便的进行node版本的切换。

非Windows系统:https://github.com/nvm-sh/nvm

Windows系统:https://github.com/coreybutler/nvm-windows

安装nvm

注意:在安装 NVM for Windows 之前卸载任何现有版本的 Node.js,否则将有冲突

双击安装程序,选择nvm安装目录 在这里插入图片描述

这个我的理解就是node的真实运行目录,通过切换不同版本,改变这个目录的文件,从而达到访问不同版本的node

在这里插入图片描述

安装Node

在运行nvm installor时nvm use,Windows通常需要管理权限(以创建符号链接)。

执行mvn得到一些帮助命令

C:\Windows\system32>nvm

Running version 1.1.9.

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 use 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 arch [32|64]:显示节点是在 32 位还是 64 位模式下运行。指定 3264 以覆盖默认架构。
nvm current:显示活动版本。
nvm install <version> [arch]:版本可以是特定版本,“latest”表示最新的当前版本,或“lts”表示最新的 LTS 版本。可选地指定是安装 32 位还是 64 位版本(默认为系统架构)。将 [arch] 设置为“all”以安装 32 位和 64 位版本。添加--insecure到此命令的末尾以绕过远程下载服务器的 SSL 验证。
nvm list [available]:列出 node.js 安装。在末尾键入available以显示可供下载的版本列表。
nvm on: 启用 node.js 版本管理。
nvm off:禁用 node.js 版本管理(不卸载任何东西)。
nvm proxy [url]:设置用于下载的代理。留空[url]以查看当前代理。设置[url]为“无”以删除代理。
nvm uninstall <version>:卸载特定版本。
nvm use <version> [arch]:切换到使用指定的版本。可选择使用latest,lts或newest. newest是最新安装的版本。可选择指定 32/64 位架构。nvm use <arch>将继续使用所选版本,但切换到 32/64 位模式。有关use在特定目录中使用(或使用.nvmrc)的信息,请参阅问题 #16。
nvm root <path>: 设置 nvm 应该存放不同版本的 node.js 的目录。如果<path>未设置,将显示当前根目录。
nvm version:显示当前运行的 NVM for Windows 版本。
nvm node_mirror <node_mirror_url>:设置节点镜像。国内的人可以使用https://npmmirror.com/mirrors/node/
nvm npm_mirror <npm_mirror_url>:设置npm镜像。中国的人可以使用https://npmmirror.com/mirrors/npm/

安装node:nvm install Node具体版本号

C:\Windows\system32>nvm install 8
Downloading node.js version 8.17.0 (64-bit)...
Complete
Creating D:\Development\nvm\temp

Downloading npm version 6.13.4... Complete
Installing npm v6.13.4...
                                           
Installation complete. If you want to use this version, type
                                            
nvm use 8.17.0

安装某个版本的node后需要开启方可使用

C:\Windows\system32>nvm list

    8.17.0
C:\Windows\system32>nvm on
nvm enabled
Now using node v8.17.0 (64-bit)

C:\Windows\system32>nvm list

  * 8.17.0 (Currently using 64-bit executable)

node版本切换

安装另一个版本的node,同时进行版本切换测试

C:\Windows\system32>nvm install 16
Downloading node.js version 16.17.0 (64-bit)...
Extracting...
Complete


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

nvm use 16.17.0

C:\Windows\system32>nvm list

    16.17.0
  * 8.17.0 (Currently using 64-bit executable)

C:\Windows\system32>nvm use 16.17.0
Now using node v16.17.0 (64-bit)

C:\Windows\system32>nvm list

  * 16.17.0 (Currently using 64-bit executable)
    8.17.0

配置

修改settings.txt文件 在这里插入图片描述 添加如下最后2行命令,提升下载速度。

root: D:\Development\nvm
path: D:\Development\nvm\nodejs

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

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

全局模块

官方提示:可能安装的任何全局 npm 模块都不会在您安装的各个版本的 node.js 之间共享。

个人有一个解决办法:

1.基于上述操作,在D:\Development\nvm目录创建modules,并执行如下命令,修改安装全局模块的安装目录。实际上这个配置信息存储在%UserProfile%\.npmrc

npm config set prefix "D:\Development\nvm\modules\node_global"

npm config set cache "D:\Development\nvm\modules\node_cache"

2.配置环境变量,基于nvm配置的环境变量进行添加全局模块的目录

在这里插入图片描述

%NVM_HOME%\modules\node_global

3.全局安装cnpm后,执行测试

npm install cnpm -g
C:\Windows\system32>node -v
v16.17.0

C:\Windows\system32>cnpm -v
cnpm@8.3.0 (D:\Development\nvm\modules\node_global\node_modules\cnpm\lib\parse_argv.js)
npm@8.19.1 (D:\Development\nvm\modules\node_global\node_modules\cnpm\node_modules\npm\index.js)
node@8.17.0 (D:\Development\nvm\nodejs\node.exe)
npminstall@6.5.1 (D:\Development\nvm\modules\node_global\node_modules\cnpm\node_modules\npminstall\lib\index.js)
prefix=D:\Development\nvm\modules\node_global
win32 x64 10.0.22000
registry=https://registry.npmmirror.com

C:\Windows\system32>nvm use 8.17.0
Now using node v8.17.0 (64-bit)

C:\Windows\system32>node -v
v8.17.0

C:\Windows\system32>cnpm -v
cnpm@8.3.0 (D:\Development\nvm\modules\node_global\node_modules\cnpm\lib\parse_argv.js)
npm@8.19.1 (D:\Development\nvm\modules\node_global\node_modules\cnpm\node_modules\npm\index.js)
node@8.17.0 (D:\Development\nvm\nodejs\node.exe)
npminstall@6.5.1 (D:\Development\nvm\modules\node_global\node_modules\cnpm\node_modules\npminstall\lib\index.js)
prefix=D:\Development\nvm\modules\node_global
win32 x64 10.0.22000
registry=https://registry.npmmirror.com