MacにNode.jsをインストールする方法についてです。
nodejsのインストール
公式サイトからダウンロードしてインストールする方法もありますが、
今回はnodebrewというのがあるのでそちらでのインストール方法を紹介します。
・公式
nodejs.org
nodeがインストールされているか確認してみます。
$ node -v
-bash: node: command not found
インストールされていないようなのでnodebrewを使ってインストールしていきます。
nodebrewのインストール
Homebrewを使用してインストールします。
Homebrewについてはこちらも参考にしてください。
www.main-function.com
以下のコマンドでnodebrewをインストールします。
$ brew install nodebrew
$ brew install nodebrew
==> Downloading https://github.com/hokaccha/nodebrew/archive/v1.0.1.tar.gz
==> Downloading from https://codeload.github.com/hokaccha/nodebrew/tar.gz/v1.0.1
######################################################################## 100.0%
==> Caveats
You need to manually run setup_dirs to create directories required by nodebrew:
/usr/local/opt/nodebrew/bin/nodebrew setup_dirs
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 completions have been installed to:
/usr/local/share/zsh/site-functions
==> Summary
🍺 /usr/local/Cellar/nodebrew/1.0.1: 8 files, 38.6KB, built in 6 seconds
とりあえずインストールされたようです。
ちなみに自分はnodebrewインストール時に以下の様なエラーが出てインストールが出来ませんでした。
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
このエラーが出た時の対処法は以下のリンクを参考にしてみてください。
www.main-function.com
nodebrewのバージョンを確認してみます。
$ nodebrew -v
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のインストールが完了したので、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
:
:
v10.0.0 v10.1.0 v10.2.0 v10.2.1 v10.3.0 v10.4.0 v10.4.1 v10.5.0
v10.6.0 v10.7.0 v10.8.0 v10.9.0 v10.10.0 v10.11.0 v10.12.0 v10.13.0
v10.14.0 v10.14.1 v10.14.2
v11.0.0 v11.1.0 v11.2.0 v11.3.0 v11.4.0
io@v1.0.0 io@v1.0.1 io@v1.0.2 io@v1.0.3 io@v1.0.4 io@v1.1.0 io@v1.2.0 io@v1.3.0
io@v1.4.1 io@v1.4.2 io@v1.4.3 io@v1.5.0 io@v1.5.1 io@v1.6.0 io@v1.6.1 io@v1.6.2
io@v1.6.3 io@v1.6.4 io@v1.7.1 io@v1.8.1 io@v1.8.2 io@v1.8.3 io@v1.8.4
io@v2.0.0 io@v2.0.1 io@v2.0.2 io@v2.1.0 io@v2.2.0 io@v2.2.1 io@v2.3.0 io@v2.3.1
io@v2.3.2 io@v2.3.3 io@v2.3.4 io@v2.4.0 io@v2.5.0
io@v3.0.0 io@v3.1.0 io@v3.2.0 io@v3.3.0 io@v3.3.1
最新バージョンをインストールしてみます。
$ nodebrew install-binary latest
Fetching: https://nodejs.org/dist/v11.4.0/node-v11.4.0-darwin-x64.tar.gz
Warning: Failed to create the file
Warning: /Users/xxxxx/.nodebrew/src/v11.4.0/node-v11.4.0-darwin-x64.tar.gz:
Warning: No such file or directory
curl: (23) Failed writing body (0 != 1059)
download failed: https://nodejs.org/dist/v11.4.0/node-v11.4.0-darwin-x64.tar.gz
フォルダがないというエラーが出ているのでフォルダを作成します。
$ mkdir -p ~/.nodebrew/src
もう一度実行してみます。
$ nodebrew install-binary latest
Fetching: https://nodejs.org/dist/v11.4.0/node-v11.4.0-darwin-x64.tar.gz
######################################################################## 100.0%
Installed successfully
インストールが完了しました。
インストールされているバージョンを確認してみます。
$ nodebrew ls
v11.4.0
current: none
カレントが設定されていないので、カレントを切り替えます。
$ nodebrew use v11.4.0
use v11.4.0
Node.jsが使用できるか確認します。
バージョンを確認します。
$ node -v
-bash: node: command not found
パスが通っていない様なので、パスを通します。
$ echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.bash_profile
ターミナルを再起動します。
Node.jsのバージョンを確認します。
$ node -v
v11.4.0
これでNode.jsのインストールは完了です。