0

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

  • Are you trying to use casperjs? If so then try `npm install casperjs`. – Jalil Sep 18 '17 at 02:28
  • I guess you only did npm instal global to enable casperjs CLI. If it is the case, you need to take adam's suggestion to do a local install as well since you need to import the module. – spiritwalker Sep 18 '17 at 02:41
  • @Jalil Yes I am using casper , there is no problem with require inside run.js , the problem only with the require inside utils.js – adam kaplan Sep 18 '17 at 02:49
  • @spiritwalker I've installed capserjs successfully , there is no problem with that ! but the problem with the require inside utils.js – adam kaplan Sep 18 '17 at 02:50
  • I think you need to install phantom too. https://sonnguyen.ws/install-nodejs-phantomjs-casperjs-ubuntu-14-04/. Check this one too. https://stackoverflow.com/questions/25761028/cannot-find-module-casper-when-invoking-through-phantomjs – Jalil Sep 18 '17 at 03:08

1 Answers1

1

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')
spiritwalker
  • 2,257
  • 14
  • 9