NVM: Node.js 多版本切换管理器的安装与使用

目录
[隐藏]

本文提到的 nvm 指的是 Node.js version management,用于 Node.js 多版本切换管理的工具。开源社区里有基于不同语言和不同系统的实现,但基本功能与用法都类似。

1. 下载与安装 nvm

1.1 windows: 下载 nvm windows 安装包

从 nvm-windows 在 github 的 releases 页面下载 setup 版本。

地址: https://github.com/coreybutler/nvm-windows/releases

下载完成后解压,执行 exe 程序进行安装。安装完成后打开 cmd 命令行,输入 nvm v,看到版本号输出,则说明安装成功。

1.2 Mac 下安装 nvm

这里介绍的是 nvm-sh/nvm 的安装示例。

使用 brew:

brew install nvm
nvm --help

从 nvm 仓库的 sh 命令安装:

curl -o- https://ghproxy.com/raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
# 或者使用 wget:
wget -qO- https://ghproxy.com/raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash

2. nvm 使用

Usage:

  nvm arch                     : Show if node is running in 32 or 64 bit mode.
  nvm install <version> [arch] : The version can be a node.js version or "latest" for the latest stable 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 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 list

# 安装指定版本的 node
nvm install v8.9.4

# 切换 node 版本
nvm use v8.9.4

# 设置默认版本node
nvm alias default v8.9.4

# 查看当前系统架构下可下载的 node 版本
nvm ls available

3. nvm 设置国内淘宝镜像

windows 下:

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

Mac 下:

vim ~/.bash_profile
// 加入以下两条配置文件
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
export NVM_IOJS_ORG_MIRROR=https://npmmirror.com/mirrors/iojs
// 重启配置文件
source ~/.bashrc

扩展参考

  • https://github.com/nvm-sh/nvm#installing-and-updating
点赞 (0)

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Captcha Code