【Mac】nodebrewのインストールとコマンドなど使い方まとめ

スポンサーリンク

f:id:tasukujp:20141130124814j:plain

NPMが必要だったのですがNode.jsがなかったのでインストールすることにしました。調べてみるとnodebrewというものがあるみたいなので使い方などをまとめておきます。

NPMとは

Node Package Manager の略で Node で作られたパッケージモジュールを管理するためのツールです。 基本 Node.js をインストールすると一緒にインストールされます。 Pythonでいう easy_install もしくは Ruby でいう gem のようなものです。

nodebrewとは

Node.jsを自分のマシン内でバージョン管理するための Tool です。Rubyのrbenvのようなものですね。

 hokaccha/nodebrew · GitHub

nodebrewのインストール

Homebrewを使ってnodebrewをインストールします。

Homebrewのインストール方法はこちら ( Homebrewのインストールと設定 - TASK NOTES )

$ brew search nodebrew
nodebrew
$ brew install nodebrew
==> Downloading https://github.com/hokaccha/nodebrew/archive/v0.7.4.tar.gz
######################################################################## 100.0%
==> /usr/local/Cellar/nodebrew/0.7.4/bin/nodebrew setup_dirs
==> Caveats
Add path:
  export PATH=$HOME/.nodebrew/current/bin:$PATH

To use Homebrew's directories rather than ~/.nodebrew add to your profile:
  export NODEBREW_ROOT=/usr/local/var/nodebrew

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completion has been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
  /usr/local/Cellar/nodebrew/0.7.4: 7 files, 44K, built in 3 seconds

インストールされたか確認します。

$ nodebrew
nodebrew 0.7.4

Usage:
    nodebrew help                         Show this message
    nodebrew install <version>            Download and install a <version> (compile from source)
    nodebrew install-binary <version>     Download and install a <version> (binary file)
...

Homebrewを使ってない人は curl コマンドを使うか

$ curl -L git.io/nodebrew | perl - setup

ダウンロードしてセットアップします。

$ wget git.io/nodebrew
$ perl nodebrew setup

nodebrewのパスを通す

nodebrewを使ってインストールするNode.jsのパスを通すために~/.bash_profileに以下を追加します。追加したらsource ~/.bash_profileを実行して再読み込みして下さい。

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

nodebrewの使い方

バージョンを確認する

インストールできるNode.jsのバージョンを確認します。

$ nodebrew ls-remote
v0.0.1    v0.0.2    v0.0.3    v0.0.4    v0.0.5    v0.0.6
...
...
v0.11.0   v0.11.1   v0.11.2   v0.11.3   v0.11.4   v0.11.5   v0.11.6   v0.11.7
v0.11.8   v0.11.9   v0.11.10  v0.11.11  v0.11.12  v0.11.13  v0.11.14

Node.jsをインストールする

インストールコマンドは次の通りです。バージョン指定もできますし、stable か latest で安定版と最新版のインストールができます。installコマンドはソースからコンパイルするため非常に遅いのでinstall-binaryを使った方がいいです。

$ nodebrew install <version>
$ nodebrew install-binary <version>
$ nodebrew install-binary stable # 安定版
$ nodebrew install-binary latest  # 最新版

切り替えも試すため3つのバージョンをインストールしました。

$ nodebrew install-binary stable
fetch: http://nodejs.org/dist/v0.10.33/node-v0.10.33-darwin-x64.tar.gz
######################################################################## 100.0%
Install successful

$ nodebrew install-binary latest
fetch: http://nodejs.org/dist/v0.11.14/node-v0.11.14-darwin-x64.tar.gz
######################################################################## 100.0%
Install successful

$ nodebrew install-binary v0.8.28
fetch: http://nodejs.org/dist/v0.8.28/node-v0.8.28-darwin-x64.tar.gz
######################################################################## 100.0%
Install successful

インストールしたNode.jsのバージョンを確認する

インストール済みの一覧を確認するには次のコマンドを使用します。

$ nodebrew list   # インストール済みのリスト
$ nodebrew ls   # listのAlias
$ nodebrew ls-all   # インストール済みのリスト + インストール可能なバージョン

先程インストールしたバージョンが表示されています。current: none なのは使用するバージョンを指定していないからです。

$ nodebrew list
v0.8.28
v0.10.33
v0.11.14

current: none

Node.jsのバージョンを指定する

使用するNode.jsのバージョンを指定するには次のコマンドを使用します。

$ nodebrew use v0.8.28
$ nodebrew use v0.6.x   # v0.6系の最新版
$ nodebrew use 0.10.29   # vは省略可能
$ nodebrew use stable   # 安定版
$ nodebrew use latest   # 最新版

v0.10の最新版を指定してみました。

$ nodebrew use v0.10.x
use v0.10.33

current にも表示されています。

$ nodebrew list
v0.8.28
v0.10.33
v0.11.14

current: v0.10.33

Node.js と npm のバージョンを確認します。ついでに npm 自身のアップデートもしておきます。

$ node -v
v0.10.33
$ npm -v
1.4.28
$ npm update -g npm

これで指定したバージョンのNode.jsが使えるようになりました。

その他コマンド

ヘルプを表示する。

$ nodebrew help

Aliasの登録・削除

特定のバージョンのAliasを登録する。unalias で削除する。

$ nodebrew alias <key> <version>
$ nodebrew unalias <key>

アンインストールと削除

uninstall でアンインストールし、clean でソースファイルを削除します。

$ nodebrew uninstall <version>
$ nodebrew clean <version>

この2つのコマンドは動作が違うので完全に消したい場合は2つ実行しましょう。

$ nodebrew clean v0.8.28    # srcファイルだけ削除される
clean v0.8.28

$ nodebrew uninstall v0.9.12    # srcファイルだけ残る
v0.9.12 uninstalled

$ ls -l ~/.nodebrew/src/
total 0
drwxr-xr-x  3 xxxxxx  staff  102 11 30 01:17 v0.10.33
drwxr-xr-x  3 xxxxxx  staff  102 11 30 01:18 v0.11.14
drwxr-xr-x  3 xxxxxx  staff  102 11 30 11:57 v0.9.12

$ ll ~/.nodebrew/node
total 0
drwxr-xr-x  9 xxxxxx  staff  306 11 30 01:17 v0.10.33
drwxr-xr-x  9 xxxxxx  staff  306 11 30 01:18 v0.11.14
drwxr-xr-x  9 xxxxxx  staff  306 11 30 01:20 v0.8.28

nodebrewのアップデート

nodebrew自身をアップデートします。

$ nodebrew selfupdate

パッケージの移動

バージョンを切り替える際に、切り替え前のNPMでインストールしたパッケージを切り替え後のバージョンにも移すことが出来ます。version には元のバージョンを指定します。

$ nodebrew migrate-package <version>

指定したバージョンのコマンドを実行

用途がよくわからないので試してません。

$ nodebrew exec <version> -- <command>