質問
nvm is a simple tool to manage multiple node.js version for each projects. I would like to show some tips to use nvm.
Update: 

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.

Installation

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

Please Install the LTS version of Node.js to set default version.

nvm install --lts

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

Write a comment

About .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>
Write a comment

Automatically load dir

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

# NVM
export NVM_DIR="$HOME/.nvm"
  [ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && . "/opt/homebrew/opt/nvm/nvm.sh"
  [ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && . "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" 

# NVM / automatically load
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
Write a comment

Special Thanks


Write a comment
この記事が気に入ったら応援お願いします🙏
12
ツイート
LINE
編集部
ゲーマーズ
ゲーム記事の編集チームです。ゲーム歴20年以上のベテランが集結しており、ゲーム雑誌への寄稿も10年近くづづけているメンバーや、eゲーム大会の上位入賞経験のあるメンバーが在籍しています。スマートフォンゲームや、PCゲームに関するご質問に何でもわかりやすく回答します!いま一番欲しいスマホは「ASUS ROG Phone 5」 です!