的require
在PhantomJS和Node.js的装置正确地与差相同,没有一个基本module的匹配。尽管该fs
module适用于两者,但它们是不同的并且不提供相同的功能。
require
在 PhantomJS 和 Node.js 中功能相同。CasperJS 建立在 PhantomJS 之上并使用其require
功能,但也对其进行了修补。使用 CasperJS 也可以要求一个module的名称,例如require('module')
代替require('./module')
它是否在同一目录中。
完整矩阵(file.js 与执行的脚本在同一目录中):
| 节点
| | 幻影
| | | 卡斯帕
| | | | 更苗条
------------+---+---+---+--------
档案 | | | | | 是 | 是
./文件 | 是 | 是 | 是 | 是
文件.js | | | | | | | n
./file.js | | | | | | | n
PhantomJS 也可以node_modules
像 node 一样使用特殊文件夹中定义的module。它不能使用依赖于 PhantomJS 中不存在的module的实际节点module。
可能需要的示例:
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
}
]
脚本.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"