1
0

More than 3 years have passed since last update.

nodebrew による Node.js の利用

Last updated at Posted at 2021-01-05

はじめに

Node.js 、および、バージョン管理システムとして nodebrew を使う場合を説明します。
これまで多くの人の解説と変わりありません。ここでは、わたしなりにまとめてみます。

利用のコンピュータ環境

$ sw_vers
ProductName:    macOS
ProductVersion: 11.1
BuildVersion:   20C69
$

nodebrewのインストール前に

すでにインストール済みの node.js と npm をアンインストールする。

たとえば、インストーラーでインストールしていた場合は次の2つのコマンドでアンインストールできる。

$ lsbom -flspf /var/db/receipts/org.nodejs.*.pkg.bom \
| while read i; do
  sudo rm /${i}
done

$ sudo rm -rf \
/usr/local/lib/node \
/usr/local/lib/node_modules \
/var/db/receipts/org.nodejs.*

Homebrewのインストール

macOS(またはLinux)用パッケージマネージャー Homebrew を使えるようにする。

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

通常は上述の1行コマンドで Homebrew を利用できるようになる。
私の場合、Homebrew を入れ直そうとしたとき、次のコマンドの実行を促されたので実行した。

$ git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
$ brew update

インストールできたか、以下のように確認できる。
もし、エラーメッセージが表示される場合は再インストール等を検討しましょう。

$ brew --version
Homebrew 2.7.1-125-gd820c9a
Homebrew/homebrew-core (git revision 8b711; last commit 2021-01-05)
Homebrew/homebrew-cask (git revision 5c3de; last commit 2021-01-04)
$ brew
Example usage:
  brew search [TEXT|/REGEX/]
  brew info [FORMULA...]
  brew install FORMULA...
  brew update
  brew upgrade [FORMULA...]
  brew uninstall FORMULA...
  brew list [FORMULA...]

Troubleshooting:
  brew config
  brew doctor
  brew install --verbose --debug FORMULA

Contributing:
  brew create [URL [--no-fetch]]
  brew edit [FORMULA...]

Further help:
  brew commands
  brew help [COMMAND]
  man brew
  https://docs.brew.sh
$ 

nodebrew のインストール

homebrew によって nodebrew をインストールする。
次のコマンドを実行:

$ brew install nodebrew

インストール後、セットアップコマンドを実行する必要があります。

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

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

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

利用しているシェルに応じて、bashなら~/.bash_profile 、zshなら~/.zprofileに次を追加:

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

zshの場合、

$ echo 'export PATH=$PATH:$HOME/.nodebrew/current/bin' >> ~/.zprofile

これを反映させる(zshの場合)

$ source ~/.zprofile

これで設定完了。

インストールできたら、nodebrew コマンドを実行してみます。

$ nodebrew

nodebrew 1.0.1

Usage:
    nodebrew help                         Show this message
    nodebrew install <version>            Download and install <version> (from binary)
    nodebrew compile <version>            Download and install <version> (from source)
    nodebrew install-binary <version>     Alias of `install` (For backword compatibility)
    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
    nodebrew install v8.9.4

    # use a specific version number
    nodebrew use v8.9.4
$ 

現在の node.js インストール状態を見る。

$ nodebrew ls              
not installed

current: none
$ 

以上から 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    

・・・省略・・・

v14.14.0  v14.15.0  v14.15.1  v14.15.2  v14.15.3  v14.15.4  

v15.0.0   v15.0.1   v15.1.0   v15.2.0   v15.2.1   v15.3.0   v15.4.0   v15.5.0
v15.5.1   

・・・省略・・・

io@v3.0.0 io@v3.1.0 io@v3.2.0 io@v3.3.0 io@v3.3.1 
$

たとえば、現行のLTS版 v14.15.4 をインストールする場合は次のとおり:

$ nodebrew install v14.15.4
Fetching: https://nodejs.org/dist/v14.15.4/node-v14.15.4-darwin-x64.tar.gz
################################################################################################################################################## 100.0%
Installed successfully
Mocha:~ akinori$ nodebrew ls
v14.15.4

current: none
$ node
-bash: node: command not found
$

current: none なので、インストールしただけで、まだ使えない。

node.js のバージョン切り替え

インストールしたバージョンの node を使う

$ nodebrew use v14.15.4
use v14.15.4
$ nodebrew ls
v14.15.4

current: v14.15.4
$ node -v
v14.15.4
$ npm -v
6.14.10
$ 

node.js, npm のインストール完了。

install コマンドを使って、複数のバージョンをインストールし、use コマンドで今利用するバージョンを指定する、という使い方ができる。

node.jsの特定バージョンのアンインストール

v14.15.4 をアンインストールする。

$ nodebrew unsinstall v14.15.4

参考

nodebrew github
https://github.com/hokaccha/nodebrew

node.js のインストール・アップデート・バージョン切替えの手順(nodebrew、Mac)
https://qiita.com/oreo3@github/items/622fd6a09d5c1593fee4

【Mac版】node.jsのアンインストールと再インストール手順メモ
https://qiita.com/wagi0716/items/94193a80502f9d81a9e0

Homebrew
https://brew.sh/index_ja

1
0
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
1
0