NVM进行NodeJS多版本管理

背景

有的时候开发环境需要多个NodeJS的版本,这个时候就可以用NVM进行管理。

安装步骤

  1. 下载NVM
    在Github里面搜索NVM,找到到NVM的项目

在这里插入图片描述
2. 找到版本1.1.7并点击下载
https://github.com/coreybutler/nvm-windows/releases/tag/1.1.7
注意,1.1.9有乱码的bug,笔者推荐使用1.1.7
在这里插入图片描述

3.下载后进行安装【window机器为例子】

4.打开cmd,执行安装不同版本Node的命令

C:\Users\35490>nvm version
1.1.9
C:\Users\35490>nvm list
No installations recognized.
C:\Users\35490>nvm install v16.10.0
Downloading node.js version 16.10.0 (64-bit)...
Extracting...
Complete
Installation complete. If you want to use this version, type
nvm use 16.10.0
C:\Users\35490>nvm list
    16.10.0
C:\Users\35490>node -v
'node' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

5.切换或者使用版本版本

nvm use 16.10.0

设置本地Node和NPM镜像

安装nvm之后,如果想把node和npm依赖包的镜像切换到taobao的话,则需要修改nvm安装目录下的setting文件,然后把下面的两行加上

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

那问题是如何找到nvm目录呢?执行下面的命令:

C:\Windows\system32>where nvm
C:\Users\35490\AppData\Roaming\nvm\nvm.exe

在这里插入图片描述

乱码解决方案

如果在执行某些nvm命令的时候,出现乱码了,比如下面,该如何解决?

C:\Users\35490>nvm use 14.20.1
exit status 5: �ܾ����ʡ�
exit status 1: ���ļ��Ѵ���ʱ���޷��������ļ���

在这里插入图片描述
解决方法是用admin管理员的权限打开cmd

nvm所有命令一览

nvm支持的命令如下:

C:\Users\35490>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非常容易的安装NodeJS不同版本并进行切换,非常的方便!

猜你喜欢

转载自blog.csdn.net/chancein007/article/details/127606830
今日推荐