Node.js (v0.8.14-release)を CentOS 6.3 にインストール

Github の master からソースコンパイルする方法。

とりあえずソースを clone する。

# cd /usr/local/src
# git clone https://github.com/joyent/node.git
# cd node

stable ビルドじゃないといろいろインストールできないっぽいので、stable ブランチに切り替える。現時点では v0.8.14-release らしい。なんでmasterが前衛的なビルドなんだろうね

# git branch -r # リモートブランチを一覧する
  origin/HEAD -> origin/master
  origin/bettercla
  origin/blog
  origin/crypto-buffers
  origin/cryptopad
  origin/debugProcess
  origin/domains
  origin/domains-wip
  origin/domains2
  origin/domains3
  origin/downloadbutton
  origin/events-monomorphic
  origin/eventsource
  origin/fix/2746
  origin/for-tomasz
  origin/isolates
  origin/issue2061
  origin/json-api-docs
  origin/master
  origin/max-tick-warning
  origin/openssl-asm
  origin/openssl-exports
  origin/openssl-fix
  origin/pathfix
  origin/perf-reg
  origin/pointer_bindings
  origin/reload
  origin/reviewme
  origin/smartos-compile-fno-strict-aliasing
  origin/speed
  origin/streams2
  origin/upgrade-gyp
  origin/v0.2
  origin/v0.4
  origin/v0.6
  origin/v0.7.10-fix
  origin/v0.7.4-release
  origin/v0.8
  origin/v0.8.10-release
  origin/v0.8.11-release
  origin/v0.8.12-release
  origin/v0.8.13-fix
  origin/v0.8.13-release
  origin/v0.8.14-release
  origin/v0.8.7-release
  origin/v0.8.8-release
  origin/v0.8.9-release
  origin/v0.9.1-release
  origin/v0.9.2-release
  origin/v0.9.3-release
  origin/v8-3.13.7.1
  origin/v8-trunk
  origin/v8upgrade
  origin/wip
  origin/writev
  origin/writev2

# git checkout -b origin/v0.8.14-release
# git branch  # ブランチ確認
  master
* origin/v0.8.14-release

コンパイルする。

# ./configure
# make
# make install

これだけ。サーバーがしょぼいとmakeで30分ぐらいかかる・・・
デフォルトだとパスはこんな感じになる

# which node
/usr/local/bin/node

そして npm も勝手に入る。

# which npm
/usr/local/bin/npm

それからバージョン確認。

# node -v
v0.8.14
# npm -v
1.1.65

JS のテストを BusterJS でやりたかったけど、なんだかうまく動かないので、PhantomJS+Jasmine を入れた。

# npm install -g phantomjs
# npm install -g jasmine-node

そしてバージョンの確認。

# phantomjs -v
1.7.0

jasmine-node はバージョンを出力するオプションがなさそうなので、下記のパッケージファイルを覗いた。

# less /usr/local/lib/node_modules/jasmine-node/package.json
"version": "1.0.26", // こんな感じの行があった

ここまで書いてなんだけど、PhantomJS + Jasmine もうまくいかなかったので、mocha にした。

# npm install mocha -g
# mocha --version
1.6.0

そして mocha も should はうまく動かない。最新のmochaだと動いてなさそうでした。mocha 0.7.0 で動いてそう。should は依然動かず。動いてる人のブログは何をしたら動いてるんだろう。とりあえず assert は上手く出来てるっぽいので、こっちでどうにかすることにした。
こちらを参考にしました。mocha と Jenkins で Node.js の CI 環境を構築する - hakobera's blog
とりあえず、上記を参考にテストが動くかどうかを確認した。下のような構成を組んだ。

mochatest
 |- lib
 |    |- calc.js
 |
 |- test
 |    |- calc.test.js
 |
 |- package.json

calc.js

function add(x, y) {
  return x+y;
}

// このへんはおまじない
if (typeof exports !== 'undefined') {
  exports.add = add;
}

calc.test.js

var calc = require('../lib/calc');
var assert = require("assert")

describe('calc', function() {
  describe('.add', function() {
    it('should return sum of 2 arguments', function() {
      var result = calc.add(1, 2);
      assert.equal(result, 3);
    });

    it('error', function() {
      var result = calc.add(1, 3);
      assert.equal(result, 3);
    });
  });
});

package.json

{
  "name": "myapp",
  "version": "0.0.1",
  "devDependencies": {
    "mocha": "0.7.0"
  }
}

package.json は依存を確認してくれる、Ruby でいうところの Rakefile みたいなもの。上記で mocha が 1.6.0 でインストールされているけど、npm install -d をやると、ルートディレクトリに node_module みたいなディレクトリができて、そこに必要なパッケージがインストールされ、そのディレクトリにいる場合は、そのローカルパッケージが使用されるようになる。
んで、テストは下記のようにする。

# npm install -d
# mocha

  ․․

  ✖ 1 of 2 tests failed:

  1) calc .add error:
     AssertionError: expected {} to equal 4
      at Object.Assertion.equal (/usr/local/src/mocha/node_modules/should/lib/should.js:295:10)
      at Context.<anonymous> (/usr/local/src/mocha/test/calc.test.js:17:21)
      at Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:200:32)
      at Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:307:10)
      at Runner.runTests.next (/usr/local/lib/node_modules/mocha/lib/runner.js:353:12)
      at next (/usr/local/lib/node_modules/mocha/lib/runner.js:235:14)
      at Runner.hooks (/usr/local/lib/node_modules/mocha/lib/runner.js:244:7)
      at next (/usr/local/lib/node_modules/mocha/lib/runner.js:192:23)
      at Runner.hook (/usr/local/lib/node_modules/mocha/lib/runner.js:212:5)
      at process.startup.processNextTick.process._tickCallback (node.js:244:9)

こんな感じになればOK。1つめのテストは成功し、2つめのテストは失敗。これでテスト側を修正すればすべてクリアになる。
別の記事で jenkins との連携の細かい部分は書くけど、レポートが欲しい場合は、下記のように書いてテストすればOK。

# mocha --reporter tap > mocha-test-results.txt