nvm: Node Version Manager [JavaScript]


nvm is a simple tool to manage multiple node.js version for each projects.

In this article, I would like to show some tips to use nvm.

😎 Install

If you use macOS and Homebrew, please run the following command:

brew install nvm
mkdir ~/.nvm

Or if you use Linux, you can use the install script using cURL:

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

If you use nvm and you put .nvmrc, you can use specific version of node.js.

😸 .nvmrc

.nvmrc specifies Node.js version in your project.

cd /path/to/project

echo "8.9.4" > .nvmrc

nvm use
Found '/path/to/project/.nvmrc' with version <9.4.0>

If you want to call automatically nvm use when you enter the directory, please write the following code in your ~/.zshrc

# Node Version Manager
export NVM_DIR="$HOME/.nvm"
. "/usr/local/opt/nvm/nvm.sh"
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

🐰 Special Thanks

🖥 Recommended VPS Service

VULTR provides high performance cloud compute environment for you. Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour). In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!