MacでNode.jsのバージョンを管理する方法を記載した内容になります。
作業内容
・Homebrewのインストール
・nodebrewのインストール
・node.jsをバージョン別にインストール
Homebrewをインストールする
こちらのHomebrewの公式ページに記載してある手順通りにインストールを行う。
ターミナルを起動して下記のコマンドを実行
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
インストールが完了したら下記コマンドでバージョンが表示されればOKです。
Homebrew 2.2.10
※2020年3月にインストールした場合のバージョンです。
nodebrewをインストールする
Homebrewがインストールできたらbrewコマンドを使用できるようになっているのでそちらを使用してnodebrewをインストールしていきます。
nodebrewをインストール
brew install nodebrew
インストールできたか確認。
nodebrew -v
下記のように表示されればインストール完了です。
nodebrew 1.0.1
Usage:
nodebrew help Show this message
nodebrew install Download and install (from binary)
nodebrew compile Download and install (from source)
nodebrew install-binary Alias of `install` (For backword compatibility)
nodebrew uninstall Uninstall
nodebrew use Use
nodebrew list List installed versions
nodebrew ls Alias for `list`
nodebrew ls-remote List remote versions
nodebrew ls-all List remote and installed versions
nodebrew alias Set alias
nodebrew unalias Remove alias
nodebrew clean | all Remove source file
nodebrew selfupdate Update nodebrew
nodebrew migrate-package Install global NPM packages contained in to current version
nodebrew exec -- Execute using specified
Example:
# install
nodebrew install v8.9.4
# use a specific version number
nodebrew use v8.9.4
環境設定
nodeが使用できるように環境パスを通します。
下記コマンドで~/.bash_profileにパスが記述されるかと思います。
echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.bash_profile
※カタリーナの場合はこちら
$ echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.zprofile
Node.jsのインストール
nodebrewをインストールできたらnodebew 〇〇でコマンドを実行できるようになります。
下記の一覧を参考にお好きなようにインストールしてみてください。
インストールできるバージョン一覧 $ nodebrew ls-remote インストール $ nodebrew install v13.11.0 安定版インストール $ nodebrew install stable 最新版インストール $ nodebrew install latest インストール済みのリスト $ nodebrew list 使用するバージョンの指定 $ nodebrew use v13.11.0 アンストール $ nodebrew uninstall削除 $ nodebrew clean
node -vで下記のようにバージョンが表示されればOKです。
v13.11.0
まとめ
node.jsのバージョンを管理しやすくすることでプロジェクトごとに別々のパッケージを保持したnodeを管理できるようになります。
最新の技術を使用しようとするとそれなりにバージョンの高いnodeが必要になり、古いプロジェクトでは低いバージョンでないと対応できなかったりします。
そんなときにnodebrewを使用することで素早くnodeのバージョンを変更できるようになるかと思います。