LoginSignup
161
93

More than 1 year has passed since last update.

Node.jsのバージョンを管理・変更する

Last updated at Posted at 2019-07-07

筆者的にはnやnodebrewよりasdfがおすすめです。(nodeに限らず多くの言語をサポートしているため)

[下準備] Homebrewでインストール済みのnodeがある場合

homebrewで管理されていたnodeを抜きます。
nodeが存在しないなどとエラーが出る場合はそのまま進んでください。
npmで管理されている場合はnpmで抜いてください。

$ brew uninstall --force node

asdfを利用するパターン

# asdfのインストール
$ brew install asdf
$ echo -e "\n. $(brew --prefix asdf)/asdf.sh" >> ~/.bash_profile
$ echo -e "\n. $(brew --prefix asdf)/etc/bash_completion.d/asdf.bash" >> ~/.bash_profile

# nodejs pluginのインストール
$ asdf plugin add nodejs
$ asdf plugin list
nodejs

# nodejsのインストール可能なバージョンを表示
$ asdf list all nodejs
0.1.14
0.1.15
0.1.16
0.1.17
...

# 特定のバージョンをインストール
$ asdf install nodejs 18.16.0

# ディレクトリ内にnodeのバージョンを適用
$ cd ./directry/path/here
$ asdf local nodejs 18.16.0

# グローバルにnodeのバージョンを適用
$ asdf global nodejs 18.16.0

🚨 注意

yarn等のグローバルなパッケージはnodejsのバージョンごとにインストールする必要があります。

# 19.9.0でyarnを使えるようにする場合
$ adsf global nodejs 19.9.0
$ npm i -g yarn

インストールしたプラグインがうまく反映されない場合

$ asdf reshim

nodebrewを利用するパターン

// 利用可能なバージョン一覧
$ nodebrew ls-remote

v8.0.0    v8.1.0    v8.1.1    v8.1.2    v8.1.3    v8.1.4    v8.2.0    v8.2.1
v8.3.0    v8.4.0    v8.5.0    v8.6.0    v8.7.0    v8.8.0    v8.8.1    v8.9.0
v8.9.1    v8.9.2    v8.9.3    v8.9.4    v8.10.0   v8.11.0   v8.11.1   v8.11.2
v8.11.3   v8.11.4   v8.12.0   v8.13.0   v8.14.0   v8.14.1   v8.15.0   v8.15.1
v8.16.0   v8.16.1   v8.16.2   v8.17.0
[...]
v13.7.0   v13.8.0   v13.9.0   v13.10.0  v13.10.1  v13.11.0  v13.12.0  v13.13.0
v13.14.0

v14.0.0   v14.1.0   v14.2.0   v14.3.0   v14.4.0
// 特定のバージョンのインストール
$ nodebrew install-binary v8.16.0

Warning: No such file or directory が出てしまったら

// 利用可能なバージョン
$ nodebrew ls
v8.16.0
v10.11.0

current: v10.11.0
// バージョンの切り替え
$ nodebrew use v8.16.0
// 最終確認
$ node -v
v8.16.0

切り替わらない場合

Warning: No such file or directory が出る場合

$ nodebrew install-binary v10.21.0
Fetching: https://nodejs.org/dist/v10.21.0/node-v10.21.0-darwin-x64.tar.gz
Warning: Failed to create the file
Warning: /Users/k3ntar0/.nodebrew/src/v10.21.0/node-v10.21.0-darwin-x64.tar.gz:
Warning:  No such file or directory

ディレクトリが無いと言われているので以下のコマンドで作成してから再度 nodebrew install-binary してみてください。

$ mkdir -p ~/.nodebrew/src

切り替わらない場合

ここまでやってバージョンが切り替わらない場合などはPATHが通っているかを疑ってください。
パッケージマネージャーを変更した場合などにnodebrewのPATHを追記する必要があるので注意してください。
macでPATHを通す方法 - zsh/bash

// .bash_profile, .zshrc 等に下記を追記して
export PATH=$HOME/.nodebrew/current/bin:$PATH

// sourceする
$ source ~/.bash_profile または ~/.zshrc

番外編 homebrewを使用したダウングレード例

// 利用可能なバージョン一覧
$ brew search node@
==> Formulae
libbitcoin-node  node ✔           node-sass        node@12          nodebrew ✔       nodenv
llnode           node-build       node@10          node_exporter    nodeenv
// node10をインストール
$ brew install node@10
// 現在のバージョン
$ node -v
v12.16.1
// node8にリンクしなおす
$ brew unlink node
$ brew link node@10

asdfを使用することをおすすめします。

161
93
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
161
93