使用NVM来管理nodejs

  我们知道nodejs官方更新的速度非常快,而我们的业务里使用的一般是某一个版本,当有新的项目进来,使用的技术栈中nodejs版本高于当前电脑安装版本,就会导致项目运行出错,因此nvm的出现,可以使我们更专注于业务的操作,把nodejs的版本控制交由nvm来处理,本文分别介绍一下在Windows和mac下面安装和使用过程。

windows下安装和使用nvm

安装前的准备

  • 检查本地是否安装nodejs

在进行安装之前,需要检查一下本地是否安装了nodejs,如果之前有安装过,需要卸载和删除nodejs以及npm的安装目录

下载安装

nvm-windows 最新下载地址:https://github.com/coreybutler/nvm-windows/releases

打开链接,可以看到有两个版本【Pre-release 1.1.6】和 【Latest release 1.1.5],我们下载当前稳定版本1.1.5就可以了。1.1.6版本是最新版本,可能还不是很稳定。

而这里又有四个可下载的文件:

  • nvm-noinstall.zip: 这个是绿色免安装版本,但是使用之前需要配置
  • nvm-setup.zip:这是一个安装包,下载之后点击安装,无需配置就可以使用,方便。
  • Source code(zip):zip压缩的源码
  • Sourc code(tar.gz):tar.gz的源码,一般用于linux系统
    笔者这里使用的是第二种

解压下载好的安装包,双击执行下载的setup文件开始安装,一路默认即可,安装完成之后,新开命令窗口输入nvm,如果出现一下提示,说明安装成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 Running version 1.1.5. 

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/npm/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.

mac下安装和使用nvm

安装之前的准备

  • 查看电脑上是否有全局安装的node/npm

如果之前是在官网下载的 node 安装包,运行后会自动安装在全局目录,其中 node 命令在 /usr/local/bin/node,npm 命令在全局 node_modules 目录中,具体路径为 /usr/local/lib/node_modules/npm

  • 安装 nvm 之前最好先删除下已安装的 node 和全局 node 模块
1
2
3
4
npm ls -g --depth=0 # 查看已经安装在全局的模块,以便删除这些全局模块后再按照不同的 node 版本重新进行全局安装
sudo rm -rf /usr/local/lib/node_modules # 删除全局 node_modules 目录
sudo rm /usr/local/bin/node # 删除 node
cd /usr/local/bin && ls -l | grep "../lib/node_modules/" | awk '{print $9}'| xargs rm # 删除全局 node 模块注册的软链

安装nvm

1
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash

详见:https://github.com/creationix/nvm

安装完 nvm 后,输入nvm,会看到有输出,则 nvm 安装成功。

1
2
3
4
5
6
7
8
9
10
11
12
13
=> Compressing and cleaning up git repository

=> Profile not found. Tried ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.
=> Create one of them and run this script again
OR
=> Append the following lines to the correct file yourself:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

如果遇到关闭shell后遇到以下提示:

1
-bash: nvm: command not found

编辑.bash_profile文件,没有的话就新建一个,命令都是:

1
vi .bash_profile

然后复制安装nvm成功时候出现的提示,保存退出:

1
2
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

然后 source 一下 .bash_profile

1
source .bash_profile

nvm常用命令

nvm install <version> ## 安装指定版本,可模糊安装,如:安装v8.10.0,既可nvm install v8.10.0

nvm uninstall <version> ## 删除已安装的指定版本,语法与install类似

nvm use <version> ## 切换使用指定的版本node

nvm ls ## 列出所有安装的版本

nvm ls-remote ## 列出所以远程服务器的版本(windows下面命令是nvm list available

nvm current ## 显示当前的版本

nvm alias default <version> ## 指定默认使用版本,不用每次新开命令行都要使用nvm use来指定版本了