Mac node.jsをインストールする

Mac node.jsをインストールする

macにnodeのインストール手順

※homebrewがインストールされているものとしてます。

環境

$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.14.5
BuildVersion: 18F132

homebrewのインストール

homebrewインストールされてない方は下記のコマンドでインストールできます。

## homebrewのインストール
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

## 確認
brew -v

<出力結果>
Homebrew 2.1.15
Homebrew/homebrew-core (git revision 4e695; last commit 2019-10-28)

homebrewは、MACのパッケージ管理システムです

nodebrewのインストール

Node.jsのバージョン管理ツールであるnodebrewからインストールする

## 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

バージョンの確認

## 確認
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

PATHの追加

インストール時のログに従い、PATHを通す

## PATHの追加
echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.bash_profile

## 反映
source ~/.bash_profile

nodeのインストール

最新版をインストール

## 最新版をインストールする
nodebrew install-binary latest

下記のエラーが発生

curl: (23) Failed writing body (0 != 820)
download failed: https://nodejs.org/dist/v13.0.1/node-v13.0.1-darwin-x64.tar.gz

ディレクトリを作成してあげれば解決

## ディレクトリ作成
mkdir -p ~/.nodebrew/src

再度インストール

nodebrew install-binary latest

<出力結果>
Fetching: https://nodejs.org/dist/v13.0.1/node-v13.0.1-darwin-x64.tar.gz
################################################################################### 100.0%
Installed successfully

成功したのでインストールしたバージョンを確認

## 確認
nodebrew ls

<出力結果>
v13.0.1

current: none

current: none(有効化されているバージョンがない) となっているので、インストールしたバージョンを有効化

## v13.0.1を使用する
nodebrew use v13.0.1

<出力結果>
use v13.0.1

再度確認

## 確認
nodebrew ls

<出力結果>
v13.0.1

current: v13.0.1

v13.0.1が使用されいる。nodeコマンドでも確認する

node -v

<出力結果>
v13.0.1

パッケージのバージョン管理ツール「npm」もnodeインストール時に、一緒にインストールされるので確認

## 確認
npm -v

<出力結果>
6.12.0

WEBでHello Worldしてみる

下記のコードをhello.jsという名前で保存する

## 作成
vi hello.js

コード

var http = require('http')
  
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'})
    res.end(`Hello World`)
}).listen(8989)

実行する

## 実行
node hello.js

ブラウザよりhttp://127.0.0.1:8989/にアクセスするとHello Worldが表示される