PhantomJSとNode.jsのrequire
はまったく同じ意味ですが、どの基本モジュールも一致しないという違いがあります。モジュールは両方に存在しfs
ますが、それらは異なり、同じ機能を提供しません。
require
PhantomJSとNode.jsで機能的に同じです。CasperJSはPhantomJSの上に構築され、そのrequire
機能を使用しますが、パッチも適用します。CasperJSを使用すると、同じディレクトリにある場合require('module')
の代わりに、などの名前のモジュールを要求することもできます。require('./module')
完全なマトリックス(file.jsは実行されたスクリプトと同じディレクトリにあります):
| ノード
| | ファントム
| | | キャスパー
| | | | スライマー
------------ + --- + --- + --- + --------
ファイル| n | n | y | y
./file | y | y | y | y
file.js | n | n | n | n
./file.js | n | n | n | n
PhantomJSはnode_modules
、ノードと同じように、特別なフォルダーで定義されたモジュールを使用することもできます。PhantomJSに存在しないモジュールに依存する実際のノードモジュールを使用することはできません。
必要なものの例:
m.js(関数用)
module.exports = function(){
return {
someKey: [1,2,3,4],
anotherKey: function(){
console.log("module exports works");
}
}
};
e.js(JSとしてのその他すべての場合)
exports.someKey = {
innerKey: [1,2,3,4]
};
exports.anotherKey = function(){
console.log("exports works");
};
a.json(任意のJSON)
[
{
"someKey": [ 1,2,3,4 ],
"anotherKey": 3
}
]
script.js
var m = require("./m")();
m.anotherKey(); // prints "module exports works"
var e = require("./e");
e.anotherKey(); // prints "exports works"
var a = require("./a");
console.log(a[0].anotherKey); // prints "3"