LoginSignup
24
24

More than 5 years have passed since last update.

【macOS Sierra】Mac OSX 10.12 macOS Sierra にNode.js 開発環境の構築 【初心者必見】

Last updated at Posted at 2016-09-25

macOS Sierra をクリーンインストールしたので、この機会にまっさらな状態からの環境構築の手順をまとめました。

標準開発ツールのインストール

まずは、Apple標準の開発ツールのインストールをします。

Xcode インストール

Mac App Store等でインストールします。

Command Line Tools for Xcode

上部メニューから Xcode → Open Developer Tool → More Developer Tools
Downloads for Apple Developerページが開くので、そこのリストから”Command Line Tools(OS X 10.12) for Xcode 8”をダウンロードしてインストール

Homebrewのインストール

Node.jsに関連するソフトウェアはパッケージ管理システムの「Homebrew」を使用してインストールします。

Homebrewとは

パッケージ管理システム Homebrew

Homebrew(ホームブルー)とは、Mac OS X オペレーティングシステム上でソフトウェアの導入を単純化するパッケージ管理システムのひとつです。

Homebrew本体のインストール
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
path設定
$ echo 'export PATH=/usr/local/bin:$PATH' >> .bash_profile
$ source .bash_profile
インストール確認
$ brew doctor
Your system is ready to brew.

※ パッケージ導入すると、いっぱいログが出てちゃんと導入できたかよくわかんなくなるので、brewのパッケージが正しく導入されているか、時々確認しておくと、いいみたいです。

nodebrewのインストール

nodebrewとは?

nodebrewは、node.jsを自分のマシン内でversion管理するためのtoolです。

※Rubyにおける「rbenv」に近いものですね。

nodebrew本体のインストール
$ brew install nodebrew
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
libyaml
==> Deleted Formulae
sgfutils

==> Using the sandbox
==> Downloading https://github.com/hokaccha/nodebrew/archive/v0.9.6.tar.gz
==> Downloading from https://codeload.github.com/hokaccha/nodebrew/tar.gz/v0.9.6
######################################################################## 100.0%
==> /usr/local/Cellar/nodebrew/0.9.6/bin/nodebrew setup_dirs
==> Caveats
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 completion has been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
🍺  /usr/local/Cellar/nodebrew/0.9.6: 7 files, 36.9K, built in 5 seconds

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

※通常では必要ない手順と思いますが、以降エラーとなったため上記コマンドでディレクトリ作成が必要でした。

path設定
$ echo ' export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.bash_profile
$ source ~/.bash_profile

Node.jsのインストール

nodebrew から Node.jsをインストールする(安定版の最新)
$ nodebrew install-binary stable
Fetching: https://nodejs.org/dist/v6.6.0/node-v6.6.0-darwin-x64.tar.gz
######################################################################## 100.0%
Installed successfully
Node.jsの使用するバージョンを指定する(安定版の最新)
$ nodebrew use stable
use v6.6.0
確認
$ nodebrew ls
v0.12.7
v6.6.0

current: v6.6.0

※上記では手順とは別のバージョンもインストール済み

サンプルアプリの作成

せっかくなので簡単な動作確認をしてみましょう。

HelloWorldを作成

以下の内容でファイル「example.js」を作成しましょう。
```
var http = require('http');

http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/')
```

サンプルアプリを起動
$ node example.js 
Server running at http://127.0.0.1:8124/

これでNode.jsが単独で立ち上がっている状態なので、

http://localhost:8124

でNode.js自体にアクセス可能です。(ブラウザで「Hello World」が表示されます。)

追記

OSXのバージョンアップ時の開発環境構築の手順をまとめています。併せてどうぞ!

【macOS Sierra】Mac OSX 10.12 macOS Sierra にRuby + Rails4 開発環境の構築 【初心者必見】
【macOS Sierra】Mac OSX 10.12 macOS Sierra にAMP環境の構築【初心者必見】【続編】

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