-
Notifications
You must be signed in to change notification settings - Fork 7
How to use Node Version Manager NVM
Erik Hetzner edited this page Apr 18, 2018
·
3 revisions
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.
-
install nvm
$brew install nvm
-
ensure ~/.nvm exists
mkdir -p ~/.nvm
-
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"
You're obviously a smart cookie. I'm sure you can figure it out.
nvm install 6.11
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
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`
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
Install https://github.com/wbyoung/avn
$ npm install -g avn avn-nvm
$ avn setup