nvm的正确打开姿势

code-wallpaper-8.jpg

昨天在运行npm install的时候突然报错,就是下面的这些话,说是是npm不支持当前版本的node.js,版本是v7.10.0需要升级,然后我就发现升级居然这么麻烦。最后发现通过nvm来管理node是如此的惬意,所以想写下这篇文章。

npm WARN npm npm does not support Node.js v7.10.0
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm can't make any promises that npm will work with this version.
npm WARN npm Supported releases of Node.js are the latest release of 6, 8, 9, 10, 11.
npm WARN npm You can find the latest version at https://nodejs.org/
Now using node v7.10.0 (npm v6.2.0)

Node.js的安装

在我们搭建react native环境的时候,官网指示使用了Homebrew来进行安装。

Homebrew, Mac系统的包管理器,用于安装NodeJS和一些其他必需的工具软件。

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

译注:在Max OS X 10.11(El Capitan)版本中,homebrew在安装软件时可能会碰到/usr/local目录不可写的权限问题。可以使用下面的命令修复:

sudo chown -R `whoami` /usr/local

然后是进行Node.js的安装,

brew install node

安装完成以后,设下下面的镜像:

npm config set registry https://registry.npm.taobao.org --global
npm config set disturl https://npm.taobao.org/dist --global

这就算搭建完成了,现在最新的react naitve要求node.js的版本是8.0以上,如果你之前安装的时候是用的7.+之类的,
然后你的同事在他的电脑上新建了一个react native的项目,那么你就会在的电脑上运行这个项目,npm install的时候,发现我上面展示的那几行错误代码。

这里就有引申出一个需求就是,根据不同的项目,需要依赖不同版本的node.js,这里就需要一个工具来管理这些node.js.那就是nvm.

Node.js的卸载

安装NVM之前你需要把你本地的电脑上的Node.js卸载了,卸载Node.js也是一个问题,你去百度一搜索Node.js无法卸载,能搜索出一堆来。

首先,打开你 Finder,按 shift+command+G,打开前往文件夹的窗口,分别输入下列目录进去之后删除 node 和 node_modules 相关的文件和文件夹:

打开 /usr/local/lib,删除 node 和 node_modules 相关的文件和文件夹
打开 /usr/local/include,删除 node 和 node_modules 相关的文件和文件夹
如果你是使用的 brew install node 安装的 NodeJS,那么你还需要在终端中执行 brew uninstall node 命令来卸载
检查你的个人主文件夹下面的所有的 local、lib 以及 include 文件夹,并且删除所有与 node 和 node_modules 相关的文件以及文件夹
打开 /usr/local/bin 并删除 node 可执行文件

同时你可以使用如下命令:

sudo rm /usr/local/bin/npm
sudo rm /usr/local/share/man/man1/node.1
sudo rm /usr/local/lib/dtrace/node.d
sudo rm -rf ~/.npm
sudo rm -rf ~/.node-gyp
sudo rm /opt/local/bin/node
sudo rm /opt/local/include/node
sudo rm -rf /opt/local/lib/node_modules

NVM的安装

Windows 上 NVM 的安装

你可以去下面这个地址去下载,记得一路往下点,如果你不想配置环境变量的话,就不要更改安装地址,如果改了安装地址的话,你需要配置一下环境变量。这个请自行百度。。。

Mac/Linux上的安装

大家可以从这个地址开始看,NVM的git上的官网地址。

运行下面的命令:

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

或者:

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

安装完以后你需要配置一下环境变量:运行下面的脚本

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

它说:

The script clones the nvm repository to ~/.nvm and adds the source line to your profile (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).

恩,就是配置环境变量,

下面你验证一下是否安装成功:

nvm -v

如果出现下面的代码:

C:\Users\feiyu>nvm -v

Running version 1.1.6.

Usage:

  nvm arch                     : Show if node is running in 32 or 64 bit mode.
  nvm install  [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       : The version must be a specific version.
  nvm use [version] [arch]     : Switch to use the specified version. Optionally specify 32/64bit architecture.
                                 nvm use  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  is not set, the current root will be displayed.
  nvm version                  : Displays the current running version of nvm for Windows. Aliased as v.


C:\Users\feiyu>

我的这个是windows版本,Mac/Linux版本也类似这样。

用NVM安装Node.js

nvm安装Node.js非常简单:

我们可以用

nvm list available  

来查看网络可以安装的版本:

C:\Users\feiyu>nvm list available

|   CURRENT    |     LTS      |  OLD STABLE  | OLD UNSTABLE |
|--------------|--------------|--------------|--------------|
|    10.7.0    |    8.11.3    |   0.12.18    |   0.11.16    |
|    10.6.0    |    8.11.2    |   0.12.17    |   0.11.15    |
|    10.5.0    |    8.11.1    |   0.12.16    |   0.11.14    |
|    10.4.1    |    8.11.0    |   0.12.15    |   0.11.13    |
|    10.4.0    |    8.10.0    |   0.12.14    |   0.11.12    |
|    10.3.0    |    8.9.4     |   0.12.13    |   0.11.11    |
|    10.2.1    |    8.9.3     |   0.12.12    |   0.11.10    |
|    10.2.0    |    8.9.2     |   0.12.11    |    0.11.9    |
|    10.1.0    |    8.9.1     |   0.12.10    |    0.11.8    |
|    10.0.0    |    8.9.0     |    0.12.9    |    0.11.7    |
|    9.11.2    |    6.14.3    |    0.12.8    |    0.11.6    |
|    9.11.1    |    6.14.2    |    0.12.7    |    0.11.5    |
|    9.11.0    |    6.14.1    |    0.12.6    |    0.11.4    |
|    9.10.1    |    6.14.0    |    0.12.5    |    0.11.3    |
|    9.10.0    |    6.13.1    |    0.12.4    |    0.11.2    |
|    9.9.0     |    6.13.0    |    0.12.3    |    0.11.1    |
|    9.8.0     |    6.12.3    |    0.12.2    |    0.11.0    |
|    9.7.1     |    6.12.2    |    0.12.1    |    0.9.12    |
|    9.7.0     |    6.12.1    |    0.12.0    |    0.9.11    |
|    9.6.1     |    6.12.0    |   0.10.48    |    0.9.10    |

This is a partial list. For a complete list, visit https://nodejs.org/download/release

然后我们通过制定版本号来安装:

nvm install 10.7.0

或者通过直接node来安装最新的发行版。

nvm install node

查看本地已经安装的版本;

nvm list (mac 是  nvm ls)

像下面这样:

C:\Users\feiyu>nvm list

  * 10.7.0 (Currently using 64-bit executable)
    8.9.3

然后通过

nvm use 版本号

来切换版本。

如果你发现在使用nvm use后,打开其他的窗口,还是之前的版本,说明还需要制定
default,不过貌似这个只在mac/linux下有这种情况。

大家可以通过设置默认的node来。

nvm alias default node

Windows 下 nvm的常用命令:

nvm arch                         查看当前系统的位数和当前nodejs的位数
nvm install  [arch]     安装制定版本的node 并且可以指定平台 version 版本号  arch 平台
nvm list [available]         
  - nvm list   查看已经安装的版本
  - nvm list installed 查看已经安装的版本
  - nvm list available 查看网络可以安装的版本
nvm on                           打开nodejs版本控制
nvm off                          关闭nodejs版本控制
nvm proxy [url]                  查看和设置代理
nvm node_mirror [url]            设置或者查看setting.txt中的node_mirror,如果不设置的默认是 https://nodejs.org/dist/
nvm npm_mirror [url]             设置或者查看setting.txt中的npm_mirror,如果不设置的话默认的是:https://github.com/npm/npm/archive/.
nvm uninstall           卸载制定的版本
nvm use [version] [arch]         切换制定的node版本和位数
nvm root [path]                  设置和查看root路径
nvm version                      查看当前的版本

Mac/Linux 下 nvm的常用命令:

nvm --help                          显示所有信息
nvm --version                       显示当前安装的nvm版本
nvm install [-s]           安装指定的版本,如果不存在.nvmrc,就从指定的资源下载安装
nvm install [-s]   -latest-npm 安装指定的版本,平且下载最新的npm
nvm uninstall              卸载指定的版本
nvm use [--silent]         使用已经安装的版本  切换版本
nvm current                         查看当前使用的node版本
nvm ls                              查看已经安装的版本
nvm ls                     查看指定版本
nvm ls-remote                       显示远程所有可以安装的nodejs版本
nvm ls-remote --lts                 查看长期支持的版本
nvm install-latest-npm              安装罪行的npm
nvm reinstall-packages     重新安装指定的版本
nvm cache dir                       显示nvm的cache
nvm cache clear                     清空nvm的cache

参考文章:
NVM的安装和命令

NVM的git官网

使用 nvm 管理不同版本的 node 与 npm

nvm-windows的git官网

Over...

你可能感兴趣的:(nvm的正确打开姿势)