In a PhantomJS script I would like to load a custom module but it seems relative paths do not works in PhantomJS ?
script.js:
var foo = require(\'./scrip
My solution to load a resource file (like let's say a json
file) within a phantomjs
subfolder from a outer folder like in this structure:
├── consumer.js
├── assets
├── data.json
├── loader.js
Supposed that data.json
must be load by the consumer
module and that this module is called by somewhere else on this machine, outside the project root folder, the fs.workingDirectory
will not work, since it will be the path of the caller file.
So to solve this, I did a simple loader
module within the assets
folder, where the files I want to load are:
(function() {
var loader = {
load : function(fileName) {
var res=require('./'+fileName);
return res;
}
}
module.exports=loader;
}).call(this);
I therefore call the loader
module from the consumer
module like
var loader=require('./data/loader');
var assets=loader.load('data.json');
and that's it.
NOTE. The require
here is the phantomjs
require not the node
version, so it works a bit differently. In this case the data.json
was a json array with no module.exports
declaration. The array will be backed in the assets
variable directly when calling the loader.load(fileName)
method.
To load your own module, the right way to do it is to use module.exports, like this: foo.js
function bar(text) {
console.log(text);
}
exports.bar = bar
And in script.js (which is executed with phantomjs script.js):
var foo = require('./script/lib/foo');
foo.bar('hello world');
phantom.exit();
have you tried to use injectJs(filename)
excerpt form PhantomJS documentation:
Injects external script code from the specified file. If the file can not be found in the current directory, libraryPath is used for additional look up.
This function returns true if injection is successful, otherwise it returns false.
After a lot of time searching for the same thing, here is what I understood, though I might be wrong :
require
, but its own require
, so things are differentrequire
, it is always interpreted as relative to the current working directory__dirname
, and as such there is no direct way to have the directory of your scriptThe solution I found least annoying :
if using phantomjs pure, without casperjs :
require(phantom.libraryPath + '/script/lib/foo.js')
if using casperjs :
var scriptName = fs.absolute( require("system").args[3] );
var scriptDirectory = scriptName.substring(0, scriptName.lastIndexOf('/'));
require(scriptDirectory + '/script/lib/foo.js')
Which PhantomJS version are you running? Support for user provided modules was added in 1.7.