念念不忘
必会回响

NVM的安装与使用

1. nvm简介

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

2. 安装nvm

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

直接下载安装程序安装即可。安装过程中会有两个目录需要选择:

  • D:\dev-tools\nvm nvm安装路径
  • D:\dev-tools\node nvm安装node的路径

3. 安装node

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

执行nvm得到一些帮助命令

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 位模式下运行。指定 32 或 64 以覆盖默认架构。
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/

4. 为什么要使用 nvm

  • 生产环境下需要使用稳定的 LTS 版本,已有的项目需要使用不同的 node.js 版本支持
  • 学习环境下需要使用最新的 latest 版本,以尝试不同的 node.js 新特性

5. 安装nvm-windows

如果本机已有Node.js,推荐先将所带Node.js卸载。
  • 访问Releases · coreybutler/nvm-windows (github.com) 点击最新版本的 nvm-setup.zip 下载到本地并安装,安装目录自定
  • 如果选择不通过 msi 安装,而是通过 便携版 安装,请下载 nvm-noinstall.zip 后,将其解压到任意文件夹下运行 install.cmd 即可,也别忘记添加 NVM_HOME(nvm安装目录) 和 NVM_SYMLINK(期望的nodejs安装目录) 到环境变量,并在 path 中添加 %NVM_HOME%;%NVM_SYMLINK%

6. 更换下载源

方法一:进入 nvm 安装目录,编辑 setting.txt,追加两行代码:

node_mirror: https://npmmirror.com/mirrors/node/
npm_mirror: https://npmmirror.com/mirrors/npm/

方法二:通过命令行配置

nvm node_mirror https://npmmirror.com/mirrors/node/
nvm npm_mirror https://npmmirror.com/mirrors/npm/

7. 安装node

nvm install Node具体版本号

例如:

 ⚡ 🦄  nvm install 18.13.0
Downloading node.js version 18.13.0 (64-bit)...
Extracting node and npm...
Complete
npm v8.19.3 installed successfully.

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

nvm use 18.13.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)

8. node切换

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

nvm install 14.7
Downloading node.js version 14.7.6 (64-bit)...
Extracting...
Complete

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

nvm use 14.7.6

nvm list

    18.13.0
  * 14.17.6 (Currently using 64-bit executable)

nvm use 18.13.0
Now using node v18.13.0 (64-bit)

nvm list

  * 18.13.0 (Currently using 64-bit executable)
    14.17.6
node -v
v18.13.0

9. 卸载指定版本

nvm uninstall 14.17.6

10. 全局模块

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

如果需要共享,可以通过如下方式:

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

npm config set prefix "D:\dev-tools\nvm\modules\node_global"

npm config set cache "D:\dev-tools\nvm\modules\node_cache"

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

%NVM_HOME%\modules\node_global

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

npm install cnpm -g

测试如下:

node -v
v16.17.0

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

nvm use 8.17.0
Now using node v8.17.0 (64-bit)

node -v
v8.17.0

cnpm -v
cnpm@8.3.0 (D:\dev-tools\nvm\modules\node_global\node_modules\cnpm\lib\parse_argv.js)
npm@8.19.1 (D:\dev-tools\nvm\modules\node_global\node_modules\cnpm\node_modules\npm\index.js)
node@8.17.0 (D:\dev-tools\nvm\nodejs\node.exe)
npminstall@6.5.1 (D:\dev-tools\nvm\modules\node_global\node_modules\cnpm\node_modules\npminstall\lib\index.js)
prefix=D:\dev-tools\nvm\modules\node_global
win32 x64 10.0.22000
registry=https://registry.npmmirror.com
赞(1) 打赏
未经允许不得转载:堆上小栈 » NVM的安装与使用

评论 抢沙发

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

登录

找回密码

注册