Proper installation and use of nvm

foreword

There are currently two mainstream node version management tools, nvm and n. The difference between the two is quite big. For a specific analysis, please refer to an article by the Taobao FED team:

Manage node versions, choose nvm or n?

In general, nvm is a bit similar to Python's virtualenv or Ruby's rvm. The modules of each node version will be installed in the sandbox of their respective version (so the modules need to be reinstalled after switching versions), so considering the need to The node version is switched to test compatibility and some modules have restrictions on the node version. I chose to use nvm as the management tool. Let's talk about the installation and use process of nvm.

Install

Here we mainly introduce the installation of mac (linux can refer to), for windows system, you can refer to windows

Deprecated installation method

At first I used homebrew to install, the installation command is as follows:

brew install nvm

After installation, in order to allow you to use nvm commands directly in the shell, you must add the following line to your .bash_profile (people who are used to putting settings in .bashrc can change the following .bash_profile to .bashrc) (Note: 如果没有这个文件,可以touch创建)

source $(brew --prefix nvm)/nvm.sh

Or directly enter the following line to join

$ echo "source $(brew --prefix nvm)/nvm.sh" >> .bash_profile

Remember to resource your .bash_profile for the settings to take effect

$ . ~/.bash_profile

OK, the above completes the installation of nvm.

Let's install node v5.12.0

nvm install 5

After successful installation, use nvm use to switch versions

nvm use v5.12.0

However the following error occurs: image

nvm use v4.6.2

is also like this:

nvm is not compatible with the npm config "prefix" option: currently set to "/Users/fabian/.nvm/versions/node/v0.12.7" Run `nvm use --delete-prefix v4.6.2` to unset it.

So you can only install the prompt, use

nvm use --delete-prefix v4.6.2

Switched successfully.

As for why the above problem occurs when installing with homebrew, please refer to an issue on Github .

Looking at the nvm documentation , it is found that the second-to-last line of the Installation section in the documentation has a sentence Homebrew installation is not supported. From this, it can be seen that it is better to look at the documentation if it is all right.

Recommended installation method

1. Uninstall node/npm that has been installed globally

如果之前是在官网下载的 node 安装包,运行后会自动安装在全局目录,其中

node 命令在 /usr/local/bin/node ,npm 命令在全局 node_modules 目录中,具体路径为 /usr/local/lib/node_modules/npm

安装 nvm 之后最好先删除下已安装的 node 和全局 node 模块:

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

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

安装完成后请重新打开终端环境,Mac 下推荐使用 oh-my-zsh 代替默认的 bash shell。 安装完成后,发现使用nvm install stable 安装node速度很慢,原因嘛,大概大家都知道我大天朝的国情。 接下来介绍如何使用国内镜像快速安装node: 把环境变量 NVM_NODEJS_ORG_MIRROR, 那么我建议你加入到 .bash_profile 文件中:

# nvm
export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node

然后你可以继续非常方便地安装各个版本的 node 了, 你可以查看一下你当前已经安装的版本:

$ nvm ls
         nvm
     v0.8.26
    v0.10.26
    v0.11.11 -> v4.3.2

三、使用 cnpm 加速 npm

同理 nvm , npm 默认是从国外的源获取和下载包信息, 不慢才奇怪. 可以通过简单的 ---registry 参数, 使用国内的镜像 https://registry.npm.taobao.org :

$ npm --registry=https://registry.npm.taobao.org install koa

于是屏幕又哗啦哗啦地一大片输出:

$ npm --registry=https://registry.npm.taobao.org install koa
npm http GET https://registry.npm.taobao.org/koa
npm http 200 https://registry.npm.taobao.org/koa
...
npm http 200 https://registry.npm.taobao.org/negotiator
npm http 200 https://registry.npm.taobao.org/keygrip
koa[@0](/user/0).5.2 node_modules/koa
├── koa-compose[@2](/user/2).2.0
├── statuses[@1](/user/1).0.2 ├── finished[@1](/user/1).1.1 ├── escape-html[@1](/user/1).0.1 ├── only[@0](/user/0).0.2 ├── debug[@0](/user/0).8.0 ├── fresh[@0](/user/0).2.2 ├── type-is[@1](/user/1).0.1 ├── delegates[@0](/user/0).0.3 ├── mime[@1](/user/1).2.11 ├── co[@3](/user/3).0.5 ├── accepts[@1](/user/1).0.1 (negotiator[@0](/user/0).4.2) └── cookies[@0](/user/0).4.0 (keygrip[@1](/user/1).0.0)

但是毕竟镜像跟官方的 npm 源还是会有一个同步时间差异, 目前 cnpm 的默认同步时间间隔是 15 分钟. 如果你是模块发布者, 或者你想马上同步一个模块, 那么推荐你安装 cnpm cli:

$ npm --registry=https://registry.npm.taobao.org install cnpm -g

通过 cnpm 命令行, 你可以快速同步任意模块:

$ cnpm sync koa connect mocha

呃, 我就是不想安装 cnpm cli 怎么办? 哈哈, 早就想到你会这么懒了, 于是我们还有一个 web 页面:

例如我想马上同步 koa, 直接打开浏览器: https://npm.taobao.org/sync/koa

或者你是命令行控, 通过 open 命令打开:

$ open https://npm.taobao.org/sync/koa

如果你安装的模块依赖了 C++ 模块, 需要编译, 肯定会通过 node-gyp 来编译, node-gyp 在第一次编译的时候, 需要依赖 node 源代码, 于是又会去 node dist 下载, 于是大家又会吐槽, 怎么 npm 安装这么慢…

好吧, 于是又要提到 --disturl 参数, 通过七牛的镜像来下载:

$ npm --registry=https://registry.npm.taobao.org --disturl=https://npm.taobao.org/mirrors/node install microtime

再次要提到 cnpm cli, 它已经默认将 --registry 和 --disturl 都配置好了, 谁用谁知道 如果不想使用cnpm cli,可以

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

nvm常用命令

  • nvm install <version> ## 安装指定版本,可模糊安装,如:安装v4.4.0,既可nvm install v4.4.0,又可nvm install 4.4

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

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

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

  • nvm ls-remote ## 列出所以远程服务器的版本(官方node version list)

  • nvm current ## 显示当前的版本

  • nvm alias <name> <version> ## 给不同的版本号添加别名

  • nvm unalias <name> ## 删除已定义的别名

  • nvm reinstall-packages <version> ## 在当前版本node环境下,重新全局安装指定版本号的npm包

附:MAC 打开.bash_profile

  1. 启动终端Terminal

  2. 进入当前用户的home目录

输入cd ~
  1. 创建.bash_profile
输入touch .bash_profile
  1. 编辑.bash_profile文件
输入open -e .bash_profile
  1. Save the file, close .bash_profile

  2. Update the environment variables you just configured

输入source .bash_profile

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326038730&siteId=291194637