NodeJS 多版本管理

NodeJS 多版本管理

背景

随着前端的快速发展,作为前端开发人员,工作中研发维护不同 NodeJS版本 的项目,是相对普遍的,那么开发机器上只有一个版本的NodeJS是无法满足的,通常的做法都是 切换环境变量 或者 重命名方式 来解决。今天我们来介绍一款,NodeJS 版本管理工具

nvm 是一款NodeJS版本管理工具,可 方便的安装、切换不同版本的NodeJS

安装步骤

  • 下载安装包 https://github.com/coreybutler/nvm-windows/releases (这里是以Windows为例)

  • 获得windows安装包 nvm-setup.exe

  • 双击安装,需要设置两个路径

    • 第一次设置NVM的安装路径, 也是后续安装不同NodeJS的路径 (eg: F:\nvm)
    • 第二次设置切换不同版本时系统链接的路径 (eg: F:\nodejs)
  • 安装完成后,打开cmd窗口,执行 nvm -version 检验是否安装成功

参数说明

C:\Users\admin>nvm -version

Running version 1.1.11.

Usage:

  nvm arch                     : Show if node is running in 32 or 64 bit mode.
  nvm current                  : Display active version.
  nvm debug                    : Check the NVM4W process for known problems (troubleshooter).
  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] : 显示node是运行在32位还是64位模式。指定32或64来覆盖默认体系结构。
nvm current: 显示当前版本
nvm install <version> [arch]: 该可以是node.js版本或最新稳定版本latest。(可选[arch])指定安装32位或64位版本(默认为系统arch)。设置[arch]为all以安装32和64位版本。在命令后面添加--insecure ,可以绕过远端下载服务器的SSL验证。
nvm list [available]: 列出已经安装的node.js版本。可选的available,显示可下载版本的部分列表。这个命令可以简写为nvm ls [available]。
nvm on: 启用node.js版本管理。
nvm off: 禁用node.js版本管理(不卸载任何东西)
nvm proxy [url]: 设置用于下载的代理。留[url]空白,以查看当前的代理。设置[url]为none删除代理。
nvm node_mirror [url]:设置node镜像,默认为https://nodejs.org/dist/.。我建议设置为淘宝的镜像https://npm.taobao.org/mirrors/node/
nvm npm_mirror [url]:设置npm镜像,默认为https://github.com/npm/npm/archive/。我建议设置为淘宝的镜像https://npm.taobao.org/mirrors/npm/
nvm uninstall <version>: 卸载指定版本的nodejs。
nvm use [version] [arch]: 切换到使用指定的nodejs版本。可以指定32/64位[arch]。nvm use <arch>将继续使用所选版本,但根据提供的值切换到32/64位模式的<arch>
nvm root [path]: 设置 nvm 存储node.js不同版本的目录 ,如果未设置,将使用当前目录。
nvm version: 显示当前运行的nvm版本,可以简写为nvm v

参数设置

  • 设置国内镜像,nvm安装目录下的settings.txt (eg: F:\nvm\settings.txt)
root: F:\nvm
path: F:\nodejs

# 增加国内镜像地址配置
node_mirror: https://npm.taobao.org/mirrors/node/ 
npm_mirror: https://npm.taobao.org/mirrors/npm/

使用说明

  • 查看可安装的NodeJS nvm list available
C:\Users\admin>nvm list available

|   CURRENT    |     LTS      |  OLD STABLE  | OLD UNSTABLE |
|--------------|--------------|--------------|--------------|
|    20.5.0    |   18.17.0    |   0.12.18    |   0.11.16    |
|    20.4.0    |   18.16.1    |   0.12.17    |   0.11.15    |
|    20.3.1    |   18.16.0    |   0.12.16    |   0.11.14    |
|    20.3.0    |   18.15.0    |   0.12.15    |   0.11.13    |
|    20.2.0    |   18.14.2    |   0.12.14    |   0.11.12    |
|    20.1.0    |   18.14.1    |   0.12.13    |   0.11.11    |
|    20.0.0    |   18.14.0    |   0.12.12    |   0.11.10    |
|    19.9.0    |   18.13.0    |   0.12.11    |    0.11.9    |
|    19.8.1    |   18.12.1    |   0.12.10    |    0.11.8    |
|    19.8.0    |   18.12.0    |    0.12.9    |    0.11.7    |
|    19.7.0    |   16.20.1    |    0.12.8    |    0.11.6    |
|    19.6.1    |   16.20.0    |    0.12.7    |    0.11.5    |
|    19.6.0    |   16.19.1    |    0.12.6    |    0.11.4    |
|    19.5.0    |   16.19.0    |    0.12.5    |    0.11.3    |
|    19.4.0    |   16.18.1    |    0.12.4    |    0.11.2    |
|    19.3.0    |   16.18.0    |    0.12.3    |    0.11.1    |
|    19.2.0    |   16.17.1    |    0.12.2    |    0.11.0    |
|    19.1.0    |   16.17.0    |    0.12.1    |    0.9.12    |
|    19.0.1    |   16.16.0    |    0.12.0    |    0.9.11    |
|    19.0.0    |   16.15.1    |   0.10.48    |    0.9.10    |

This is a partial list. For a complete list, visit https://nodejs.org/en/download/releases
  • 安装指定版本 nvm install 20.5.0 64
C:\Users\admin>nvm install 20.5.0 64
Downloading node.js version 20.5.0 (64-bit)...
Extracting node and npm...
Complete
npm v9.8.0 installed successfully.


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

nvm use 20.5.0
  • 查看安装路径
C:\Users\admin>cd /d F:\nvm

F:\nvm>dir
 驱动器 F 中的卷是 Tools
 卷的序列号是 14C5-A59F

 F:\nvm 的目录

2023/08/07  10:25    <DIR>          .
2023/08/07  10:25    <DIR>          ..
2021/12/07  12:36               305 elevate.cmd
2021/12/07  12:36               335 elevate.vbs
2023/01/26  21:28               724 install.cmd
2021/12/07  12:36            15,086 nodejs.ico
2023/08/07  10:25    <DIR>          node_cache
2023/08/07  10:25    <DIR>          node_modules
2023/04/12  22:30         8,294,304 nvm.exe
2023/08/07  10:13               134 settings.txt
2023/02/13  20:54               384 setuserenv.vbs
2023/08/07  09:49            18,553 unins000.dat
2023/08/07  09:47         3,048,509 unins000.exe
2023/02/13  20:54               286 unsetuserenv.vbs
2023/08/07  10:21    <DIR>          v20.5.0
              10 个文件     11,378,620 字节
               5 个目录 108,373,352,448 可用字节
# v20.5.0 目录即是刚才安装的NodeJS
  • 查看已安装的版本 nvm list
C:\Users\admin>nvm list

    20.5.0
  • 切换版本 nvm use 20.5.0
C:\Users\admin>nvm use 20.5.0
Now using node v20.5.0 (64-bit)
  • 查看npm版本 npm -v

    • 执行 npm -v
    C:\Users\admin>npm -v
    'npm' 不是内部或外部命令,也不是可运行的程序或批处理文件。
    
    • 设置环境变量 NODE_PATH=F:\nodejs
    • 重新打开命令行窗口, 执行 npm -v
    C:\Users\admin>npm -v
    9.8.0
    
  • 剩余其他操作和原来的保持一致

综上所述,NodeJS版本管理工具 nvm 则介绍完毕,希望对大家有所帮助。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜逼の世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值