node.js で Google Closure Library を使う。

node-goog を使おうとしたときにエラーがでたのでメモ。
GitHub - hsch/node-goog: Server-side Google Closure with Node.js
まず、npm で node-goog をインストール。

$npm install goog

で、環境変数でClosure Library が置いてあるディレクトリを設定する。(base.js が置いてあるディレクトリを指定する。zipをダウンロードした場合は。colsure/goog のはず。ここではzipをホームディレクトリに展開したものとする。)

export CLOSURE_BASE_PATH=~/closure/goog/

ここまでで、node を起動して goog をrequireすると以下のとおりエラーになる。

> var goog = require('goog').goog;
TypeError: Cannot call method 'runInNewContext' of undefined
    at loadScript (*****/node_modules/goog/lib/goog.js:53:10)
    at *****/node_modules/goog/lib/goog.js:81:2
    at Object.<anonymous> (*****/node_modules/goog/lib/goog.js:106:2)
    at Module._compile (module.js:423:26)
    at Object..js (module.js:429:10)
    at Module.load (module.js:339:31)
    at Function._load (module.js:298:12)
    at require (module.js:367:19)
    at [object Context]:1:12
    at Interface.<anonymous> (repl.js:168:22)

runInNewContext がないというので goog.js の中を見てみると 18行目あたりに、以下のコードがあるので、

var Script = process.binding('evals').Script;

これを、以下のように変更する。

var Script = process.binding('evals').NodeScript;

というか。
1ヶ月ほど前にメモっておいて公開し忘れていたものなので、node-goog 側ですでに修正済みかもしれない。