node版本管理工具nvm的安装与使用

介绍

nvm是一个node.js的版本管理器,全程“node version manager”。使用nvm可以通过一个简单的命令快速安装、切换node.js版本。

安装

linux系统直接脚本运行安装即可。
官方安装教程请见:https://github.com/nvm-sh/nvm#installing-and-updating

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

windows系统需要安装nvm-windows,官方地址:https://github.com/coreybutler/nvm-windows
建议下载setup版本的安装程序:
https://github.com/coreybutler/nvm-windows/releases
在这里插入图片描述
下载以后运行安装即可。如果本机已经安装了node.js,安装过程中会提示是否需要让nvm管理已经安装的node.js,建议选择允许,把node.js交给nvm管理。

使用

终端直接输入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.

查看当前node版本:

nvm current

安装最新版本node.js:

nvm install latest

安装指定版本node.js:
<version>更换为版本号,比如:16.14.2

nvm install <version>

查看当前系统有哪些node.js版本:

nvm list

卸载指定版本:

nvm uninstall

切换node.js版本:

nvm use