[Sy] Raspbian に nodebrew を使って node と npm をインストールする
      
        2017/12/24
        
      
    
    Raspbian に node.js の環境を構築する手順です。 mac や linux では nodebrew が気に入ってるので、 nodebrew を使って構築します。
nodebrewの README どおりに進めます。
0. 確認
まずデフォルトの状態だと、Raspbian には node がインストールされてました。 npm はなかったです。
$ node -v
v4.8.2
$ npm -v
zsh: command not found: npm
1. nodebrew をインストール
次のコマンドで nodebrew をインストールします。
$ curl -L git.io/nodebrew | perl - setup
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
100 24507  100 24507    0     0  10720      0  0:00:02  0:00:02 --:--:-- 10720
Fetching nodebrew...
Installed nodebrew in $HOME/.nodebrew
========================================
Export a path to nodebrew:
export PATH=$HOME/.nodebrew/current/bin:$PATH
========================================
ホームディレクトリの下に、 .nodebrew というディレクトリが作られます。
続いて、 .bashrc や .zshrc を編集して $HOME/.nodebrew/current/bin に PATH をとおします。
$ vim ~/.zshrc
(.zshrc)
・
・
・
export PATH=$HOME/.nodebrew/current/bin:$PATH
編集して保存したら、シェルを再起動するなどして変更を反映させます。
ここまででインストールは完了です。
nodebrew help で動作確認してみましょう。
$ nodebrew help
nodebrew 0.9.8
Usage:
    nodebrew help                         Show this message
    nodebrew install <version>            Download and install <version> (compile from source)
    nodebrew install-binary <version>     Download and install <version> (binary file)
    nodebrew uninstall <version>          Uninstall <version>
    nodebrew use <version>                Use <version>
    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 <key> <value>          Set alias
    nodebrew unalias <key>                Remove alias
    nodebrew clean <version> | all        Remove source file
    nodebrew selfupdate                   Update nodebrew
    nodebrew migrate-package <version>    Install global NPM packages contained in <version> to current version
    nodebrew exec <version> -- <command>  Execute <command> using specified <version>
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 io@v1.0.0
    nodebrew use io@v1.0.0
ヘルプが表示されればOKです。
2. node と npm をインストール
nodebrew がインストールできたので、現時点(2017/12/24)で最新の安定版(v8.x.x)の node をインストールします。
まず以下のコマンドで、インストールできるバージョンのリストを取得します。
$ nodebrew ls-remote | grep v8
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.3 が最新なので、これをインストールします。
インストールには、 nodebrew install-binary コマンドを使うと早くて良いです。
$ nodebrew install-binary v8.9.3
Fetching: https://nodejs.org/dist/v8.9.3/node-v8.9.3-linux-armv7l.tar.gz
######################################################################## 100.0%
Installed successfully
すぐにインストールは終わります。
3. node のバージョンを切り替える
最後に、先ほどインストールした v8.9.3 に切り替えます。
$ nodebrew use v8.9.3
node と npm のバージョンを確認します。
$ node -v
v8.9.3
$ npm -v
5.5.1