LoginSignup
6
4

nodebrewのセットアップ&操作方法(Mac)

Last updated at Posted at 2019-12-19

「nodebrew」とは?

Node.jsのバージョンを管理するツールです。
rbenvやpyenvのNode.js版と考えるとわかりやすいです。

環境

  • OS:macOS Mojave 10.14.6
  • nodebrew:1.0.1

セットアップ

nodebrewのインストール

Homebrewからインストールします。

$ brew install nodebrew

nodebrewの初期設定

nodebrew setup を実行するのみです。

$ nodebrew setup
Fetching nodebrew...
Installed nodebrew in $HOME/.nodebrew

========================================
Export a path to nodebrew:

export PATH=$HOME/.nodebrew/current/bin:$PATH
========================================

出力内容に従って ~/.bash_profile に以下を追記し、パスを通します。

.bash_profile
+ export NODEBREW_DIR="${HOME}/.nodebrew"
+ if [ -d "${NODEBREW_DIR}" ]; then
+   export PATH=$NODEBREW_DIR/current/bin:$PATH
+ fi

PATH$PATH:$NODEBREW_DIR/current/bin のように書くと、MacにプリインストールされているNode.jsが優先されることがあるようです。

~/.bash_profile をリフレッシュします。

$ source ~/.bash_profile

操作方法

Node.jsのインストール

以下のコマンドを実行し、任意のバージョンのNode.jsをインストールします。

# インストールできるNode.jsのバージョンを確認する
$ nodebrew ls-remote
v0.0.1    v0.0.2    v0.0.3    v0.0.4    v0.0.5    v0.0.6
…
v12.0.0   v12.1.0   v12.2.0   v12.3.0   v12.3.1   v12.4.0   v12.5.0   v12.6.0
v12.7.0   v12.8.0   v12.8.1   v12.9.0   v12.9.1   v12.10.0  v12.11.0  v12.11.1
v12.12.0  v12.13.0  v12.13.1  v12.14.0  

v13.0.0   v13.0.1   v13.1.0   v13.2.0   v13.3.0   v13.4.0   v13.5.0
…

# 最新版をインストールする
$ nodebrew install-binary v13.5.0

nodebrew setup を実行していないと、以下のエラーが発生します。

$ nodebrew install-binary latest
Fetching: https://nodejs.org/dist/v11.12.0/node-v11.12.0-darwin-x64.tar.gz
Warning: Failed to create the file
Warning: /Users/{ユーザー名}/.nodebrew/src/v11.12.0/node-v11.12.0-darwin-x64.tar
Warning: .gz: No such file or directory
0.0%
curl: (23) Failed writing body (0 != 1057)
download failed: https://nodejs.org/dist/v11.12.0/node-v11.12.0-darwin-x64.tar.gz

Node.jsのバージョン切替

nodebrew use コマンドでNode.jsのバージョンを設定します。

$ nodebrew use v13.5.0

Node.jsのバージョン確認

nodebrew list コマンドでインストールされているNode.jsの全バージョンを確認できます。
「current」が設定しているバージョンです。

$ nodebrew list
v13.5.0

current: v13.5.0

実際のNode.jsのバージョンも確認します。
currentとバージョンが異なる場合、nodebrewでインストールしているNode.jsが使われていない可能性があります。

$ node -v
v13.5.0

Node.jsのアンインストール

nodebrew uninstall コマンドでNode.jsをアンインストールします。

$ nodebrew list
v16.15.0
v16.15.1

current: v16.15.1

$ nodebrew uninstall v16.15.0
v16.15.0 uninstalled

$ nodebrew list
v16.15.1

current: v16.15.1

コラム:LTSとCurrent

Node.jsにはLTS(Long-term support)とCurrentがあります。
基本的に奇数バージョン(v11, 13など)はLTSにならないため、業務では偶数バージョン(v10, 12など)を使うことが多いと思います。

2019/12/19現在、LTSの最新バージョンが12.14.0、Currentの最新バージョンが13.5.0です。

nodebrewでは stable でLTSの最新バージョン、 latest でCurrentの最新バージョンを指定できます。
つまり、業務でバージョンの細かい指定が不要なら nodebrew install-binary stable でインストールすればOKです。

参考リンク

6
4
0

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
6
4