Windows版 Node.js である node.exe がリリースされたことは先日の記事 id:waritohutsu:20110801:1312224615 にて記述し、同記事にて Windows Azure Platform で動作させるまでの手順を紹介した。今回は node.exe でライブラリを読み込む TIPS について紹介する。
Node.js のライブラリとは?
Node.js はデータベース接続やファイル操作、ネットワーク接続が可能だが、同機能は個別にライブラリを読み込む必要がある。ライブラリは当然JavaScriptで記述されており、以下の書式で読み込むことが可能だ。
var http = require('http'); var sys = require('sys'); var net = require('net'); var fs = require('fs');
上記のライブラリは Node.js のランタイム(Windows版では node.exe モジュール)に含まれているためにエラーが発生しないが、Node.js ランタイム内に含まれないサードパーティ製のライブラリ(例では sax )を読み込もうとすると以下のエラーが発生する。
C:\my_program\JavaScript\Node.js>node.exe example_lib.js node.js:189 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: Cannot find module 'sax' at Function._resolveFilename (module.js:317:11) at Function._load (module.js:262:25) at Module.require (module.js:340:17) at require (module.js:351:17) at Object.<anonymous> (C:\my_program\JavaScript\Node.js\example_lib.js:5:10) at Module._compile (module.js:411:26) at Object..js (module.js:450:10) at Module.load (module.js:334:31) at Function._load (module.js:293:12) at Array.<anonymous> (module.js:463:10)
これを回避するためには、ライブラリの読み込みパスを追加する必要がある。
ライブラリの読み込みパスを追加する
Node.js のサードパーティ製のライブラリを読み込むため、ライブラリの読み込みパスを追加するには二つの方法がある。
- NODE_PATH 環境変数に値を追加する
- require.paths.push() メソッドの引数にパスを追加する
NODE_PATH 環境変数に値を追加する
NODE_PATH 環境変数を設定するソースコードとコマンドを記載する。
console.log("------------------require.paths------------------");
console.log(require.paths);
- コマンド
C:\my_program\JavaScript\Node.js>set NODE_PATH=.:/tmp <---------- なぜか Win版なのに : 区切りな不思議 C:\my_program\JavaScript\Node.js>node.exe example_lib.js ------------------require.paths------------------ [ '.', '/tmp', 'C:\\my_program\\JavaScript\\lib\\node' ] <-------- 通常はここだけ。何故か実行パスと違う不思議
上記の様に実行される。不思議な個所が二箇所程あるが、私も理解できていないので突っ込みを期待する。
require.paths.push() メソッドの引数にパスを追加する
require.paths.push() メソッドを利用すれば、実行中にライブラリの読み込みパスを追加することが可能だ。以下にコードとコマンドを記述する。
console.log("------------------require.paths------------------"); console.log(require.paths); console.log("\n------------------after push, require.paths -----"); require.paths.push('C:\\node\\lib'); console.log(require.paths);
- コマンド
C:\my_program\JavaScript\Node.js>node.exe example_lib.js ------------------require.paths------------------ [ 'C:\\my_program\\JavaScript\\lib\\node' ] ------------------after push, require.paths ----- [ 'C:\\my_program\\JavaScript\\lib\\node', 'C:\\node\\lib' ]
サードパーティ製のライブラリを読み込む
上記どちらかの方法でパスを追加したのち、サードパーティ製のライブラリを読み込む。「C:\\node\\lib\\sax.js」を配置することで、以下のコードがエラーなく実行できた。
console.log("------------------require.paths------------------"); console.log(require.paths); require.paths.push('C:\\node\\lib'); var fs = require('sax'); console.log("\n------------------after push, require.paths -----"); console.log(require.paths);
- コマンド
C:\my_program\JavaScript\Node.js>node.exe example_lib.js ------------------require.paths------------------ [ 'C:\\my_program\\JavaScript\\lib\\node' ] ------------------after push, require.paths ----- [ 'C:\\my_program\\JavaScript\\lib\\node', 'C:\\node\\lib' ]