Search code examples
javascriptrequirejscasperjsrequire

How to require module within nested module ? javascript


I have the following project structure :

WEBCONTENT /run.js
            utils /utils.js

Here is run.js file:

var casper = require("casper").create();
var utils = require("../utils/utils");

Here is utils.js file:

try{
 var x = require("casper").selectedPath;
}catch(error){
 console.log(error);//cannot find module casper
}

once I run the bellow command :

casperjs run.js

I receive this error : cannot find module casper I tried other paths such as ../casper , ./casper . but no luck !

please tell me how can I use require properly inside utils.js ? thanks

PLEASE NOTE , I installed casperjs successfully, there is no problem with that ! since the require in run.js works , however the require in utils.js crash


Solution

  • sorry I thought this was a nodejs question at the first place and left some misleading comments. It turns out this is really phantomJS related.

    So the easiest solution is to use patchRequire for all native requires(all casper related modules for instance. The following code works for me.

    var require = patchRequire(require)
    var casper = require('casper')