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
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')