Skip to content

How to use Node Version Manager NVM

Erik Hetzner edited this page Apr 18, 2018 · 3 revisions

How-to use Node Version Manager (NVM)

Introduction

NVM is a tool used for managing multiple Node.js versions on a single machine. If you're coming from Ruby, it's purpose is very similar to RVM, Rbenv, or Chruby.

Installation

Mac + homebrew

  1. install nvm

    $brew install nvm
  2. ensure ~/.nvm exists

    mkdir -p ~/.nvm
  3. add the following to your ~/.bash_profile or ~/.zshrc (depending on whether you use bash or zsh):

    export NVM_DIR="$HOME/.nvm"
    . "/usr/local/opt/nvm/nvm.sh"

Linux

You're obviously a smart cookie. I'm sure you can figure it out.

Usage

source

Installing a node version

nvm install 6.11

Using a node version

nvm use 6.11

or if you're in a directory which has an .nvmrc in or above it. You can simply run

nvm use

and nvm will select the node version specified in the .nvmrc

Deeper shell integration

This will automatically switch node versions when you enter a directory (like what rvm does). I highly recommend setting this up since it's easy to forget to type `nvm use`

zsh

source

add this to your .zshrc

# place this after nvm initialization!
autoload -U add-zsh-hook
load-nvmrc() {
  local node_version="$(nvm version)"
  local nvmrc_path="$(nvm_find_nvmrc)"

  if [ -n "$nvmrc_path" ]; then
    local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")

    if [ "$nvmrc_node_version" = "N/A" ]; then
      nvm install
    elif [ "$nvmrc_node_version" != "$node_version" ]; then
      nvm use
    fi
  elif [ "$node_version" != "$(nvm version default)" ]; then
    echo "Reverting to nvm default version"
    nvm use default
  fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc

bash

Install https://github.com/wbyoung/avn

$ npm install -g avn avn-nvm
$ avn setup
Clone this wiki locally