macにnodebrewを使ってnode.jsをインストール

開発環境はDockerをつかっていますが、node_modulesをvolumesでマウントするとエラーになったりして動かないことがあるのでコンテナ上だけでなく、Macのほうにもnode、npmを入れてnode_modulesをインストールしたい。

nodebrew

nodebrewは、nodeのバージョンを管理やバージョンの切り替えを行えるツール

すでにnode.jsがパッケージでインストールされいたいたら削除しよう

インストールされているか確認

node -v

もしバージョンが表示されていたら、「node.jsの削除」、「NPMの削除」を実行する

node.jsの削除

lsbom -f -l -s -pf /var/db/receipts/org.nodejs.node.pkg.bom \
| while read i; do
sudo rm /usr/local/${i}
done
sudo rm -rf /usr/local/lib/node \
/usr/local/lib/node_modules \
/var/db/receipts/org.nodejs.*

NPMの削除

sudo rm -rf ~/.npm

homebrewのインストール

nodebrewをインストールする前にまずhomebrewをインストールする。
homebrewはmacOS用のパッケージマネージャー(https://brew.sh/index_ja.html)です。

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

nodebrewのインストール

brew install nodebrew

nodebrewの確認

nodebrew -v

バージョンとnodebrewの使い方が表示されればOK

nodebrew 0.9.7

Usage:
nodebrew help Show this message
nodebrew install Download and install (compile from source)
nodebrew install-binary Download and install (binary file)
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 -- <command></command> Execute <command></command> using specified

Example:
# install from binary
nodebrew install-binary v0.10.22

# use a specific version number
nodebrew use v0.10.22

# io.js
nodebrew install-binary [email protected]
nodebrew use [email protected]

node.jsのインストール

nodebrewのインストールが完了したので、node.jsのインストール手順です。

node.jsの最新をインストール

nodebrew install-binary latest

インストールできないこんなエラーになった場合

Fetching: https://nodejs.org/dist/v8.3.0/node-v8.3.0-darwin-x64.tar.gz
Warning: Failed to create the file
Warning: /Users/HogeHoge/.nodebrew/src/v8.3.0/node-v8.3.0-darwin-x64.tar.gz: No
Warning: such file or directory
0.0%
curl: (23) Failed writing body (0 != 941)
download failed: https://nodejs.org/dist/v8.3.0/node-v8.3.0-darwin-x64.tar.gz

ディレクトリがないと怒られているので、ディレクトリを作成

mkdir ~/.nodebrew/src

もう一度、node.jsの最新をインストールするコマンドを実行

nodebrew install-binary latest

インストールできた。

Fetching: https://nodejs.org/dist/v8.3.0/node-v8.3.0-darwin-x64.tar.gz
######################################################################## 100.0%
Installed successfully

インストール済みのnode.jsを確認

nodebrew list

確認できたけど、currentは設定されていないな

nodebrew list
v8.3.0

current: none

インストール可能なnode.jsのバージョンを確認

nodebrew ls-remote

たくさんあります。

v0.0.1 v0.0.2 v0.0.3 v0.0.4 v0.0.5 v0.0.6

v0.1.0 v0.1.1 v0.1.2 v0.1.3 v0.1.4 v0.1.5 v0.1.6 v0.1.7
v0.1.8 v0.1.9 v0.1.10 v0.1.11 v0.1.12 v0.1.13 v0.1.14 v0.1.15
v0.1.16 v0.1.17 v0.1.18 v0.1.19 v0.1.20 v0.1.21 v0.1.22 v0.1.23
v0.1.24 v0.1.25 v0.1.26 v0.1.27 v0.1.28 v0.1.29 v0.1.30 v0.1.31
v0.1.32 v0.1.33 v0.1.90 v0.1.91 v0.1.92 v0.1.93 v0.1.94 v0.1.95
v0.1.96 v0.1.97 v0.1.98 v0.1.99 v0.1.100 v0.1.101 v0.1.102 v0.1.103
v0.1.104

v0.2.0 v0.2.1 v0.2.2 v0.2.3 v0.2.4 v0.2.5 v0.2.6

v0.3.0 v0.3.1 v0.3.2 v0.3.3 v0.3.4 v0.3.5 v0.3.6 v0.3.7
v0.3.8

v0.4.0 v0.4.1 v0.4.2 v0.4.3 v0.4.4 v0.4.5 v0.4.6 v0.4.7
v0.4.8 v0.4.9 v0.4.10 v0.4.11 v0.4.12

v0.5.0 v0.5.1 v0.5.2 v0.5.3 v0.5.4 v0.5.5 v0.5.6 v0.5.7
v0.5.8 v0.5.9 v0.5.10

v0.6.0 v0.6.1 v0.6.2 v0.6.3 v0.6.4 v0.6.5 v0.6.6 v0.6.7
v0.6.8 v0.6.9 v0.6.10 v0.6.11 v0.6.12 v0.6.13 v0.6.14 v0.6.15
v0.6.16 v0.6.17 v0.6.18 v0.6.19 v0.6.20 v0.6.21

v0.7.0 v0.7.1 v0.7.2 v0.7.3 v0.7.4 v0.7.5 v0.7.6 v0.7.7
v0.7.8 v0.7.9 v0.7.10 v0.7.11 v0.7.12

v0.8.0 v0.8.1 v0.8.2 v0.8.3 v0.8.4 v0.8.5 v0.8.6 v0.8.7
v0.8.8 v0.8.9 v0.8.10 v0.8.11 v0.8.12 v0.8.13 v0.8.14 v0.8.15
v0.8.16 v0.8.17 v0.8.18 v0.8.19 v0.8.20 v0.8.21 v0.8.22 v0.8.23
v0.8.24 v0.8.25 v0.8.26 v0.8.27 v0.8.28

v0.9.0 v0.9.1 v0.9.2 v0.9.3 v0.9.4 v0.9.5 v0.9.6 v0.9.7
v0.9.8 v0.9.9 v0.9.10 v0.9.11 v0.9.12

v0.10.0 v0.10.1 v0.10.2 v0.10.3 v0.10.4 v0.10.5 v0.10.6 v0.10.7
v0.10.8 v0.10.9 v0.10.10 v0.10.11 v0.10.12 v0.10.13 v0.10.14 v0.10.15
v0.10.16 v0.10.17 v0.10.18 v0.10.19 v0.10.20 v0.10.21 v0.10.22 v0.10.23
v0.10.24 v0.10.25 v0.10.26 v0.10.27 v0.10.28 v0.10.29 v0.10.30 v0.10.31
v0.10.32 v0.10.33 v0.10.34 v0.10.35 v0.10.36 v0.10.37 v0.10.38 v0.10.39
v0.10.40 v0.10.41 v0.10.42 v0.10.43 v0.10.44 v0.10.45 v0.10.46 v0.10.47
v0.10.48

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 v0.11.15
v0.11.16

v0.12.0 v0.12.1 v0.12.2 v0.12.3 v0.12.4 v0.12.5 v0.12.6 v0.12.7
v0.12.8 v0.12.9 v0.12.10 v0.12.11 v0.12.12 v0.12.13 v0.12.14 v0.12.15
v0.12.16 v0.12.17 v0.12.18

v4.0.0 v4.1.0 v4.1.1 v4.1.2 v4.2.0 v4.2.1 v4.2.2 v4.2.3
v4.2.4 v4.2.5 v4.2.6 v4.3.0 v4.3.1 v4.3.2 v4.4.0 v4.4.1
v4.4.2 v4.4.3 v4.4.4 v4.4.5 v4.4.6 v4.4.7 v4.5.0 v4.6.0
v4.6.1 v4.6.2 v4.7.0 v4.7.1 v4.7.2 v4.7.3 v4.8.0 v4.8.1
v4.8.2 v4.8.3 v4.8.4

v5.0.0 v5.1.0 v5.1.1 v5.2.0 v5.3.0 v5.4.0 v5.4.1 v5.5.0
v5.6.0 v5.7.0 v5.7.1 v5.8.0 v5.9.0 v5.9.1 v5.10.0 v5.10.1
v5.11.0 v5.11.1 v5.12.0

v6.0.0 v6.1.0 v6.2.0 v6.2.1 v6.2.2 v6.3.0 v6.3.1 v6.4.0
v6.5.0 v6.6.0 v6.7.0 v6.8.0 v6.8.1 v6.9.0 v6.9.1 v6.9.2
v6.9.3 v6.9.4 v6.9.5 v6.10.0 v6.10.1 v6.10.2 v6.10.3 v6.11.0
v6.11.1 v6.11.2

v7.0.0 v7.1.0 v7.2.0 v7.2.1 v7.3.0 v7.4.0 v7.5.0 v7.6.0
v7.7.0 v7.7.1 v7.7.2 v7.7.3 v7.7.4 v7.8.0 v7.9.0 v7.10.0
v7.10.1

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

バージョンを指定してインスール

nodebrew install-binary 7.10.1

確認

nodebrew list

インストールされている

v7.10.1
v8.3.0

current: none

node.jsのバージョンを有効にする

nodebrew use v8.3.0

確認

nodebrew list
v7.10.1
v8.3.0

current: v8.3.0

bashrcへパスを追加する

echo 'export PATH=$PATH:/Users/★★★/.nodebrew/current/bin' &gt;&gt; ~/.bashrc

もしかして~/.bash_profileに~/.bashrcを読み込む処理がなかったら以下を追加

if [ -f ~/.bashrc ] ; then
. ~/.bashrc
fi