Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using require with casperjs returns "Can't find module" error

I'm following the documentation on writing a module with casperjs, but I'm getting an error.

The code, straight from the docs:

// my module, stored in universe.js
// patching phantomjs' require()
var require = patchRequire(require);

// now you're ready to go
var utils = require('utils');
var magic = 42;
exports.answer = function() {
    return utils.format("it's %d", magic);
};

And the calling script:

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


console.log(universe.answer());

casper.test.begin("Home Page", 1, function suite(test) {

});

But I get the following error:

casperjs test testStage.js
Test file: testStage.js
CasperError: Can't find module ./universe
  /usr/local/Cellar/casperjs/1/libexec/bin/bootstrap.js:214 in patchedRequire
  /Users/smosk/Google Drive/source/nest/testStage.js:1
FAIL CasperError: Can't find module ./universe
#    type: error
#    file: testStage.js
#    subject: false
#    error: "CasperError: Can't find module ./universe"
#    stack: in patchedRequire() in /usr/local/Cellar/casperjs/1/libexec/bin/bootstrap.js:214
in anonymous() in testStage.js:1
FAIL 1 test executed in 0.031s, 0 passed, 1 failed, 0 dubious, 0 skipped.

Details for the 1 failed test:

In testStage.js
  Untitled suite in testStage.js
    error: CasperError: Can't find module ./universe

Also tried defining casper first:

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

But that just resulted in

Test file: testStage.js
CasperError: Can't find module ./universe
  /usr/local/Cellar/casperjs/1/libexec/bin/bootstrap.js:214 in patchedRequire
  /Users/smosk/Google Drive/source/nest/testStage.js:2
like image 708
Simon Avatar asked Aug 21 '13 01:08

Simon


4 Answers

You should try to upgrade to the lastest master build of CasperJS as this pull request that I made should fix the problem that you're having.

However, if you don't want to upgrade, you could also try running CasperJS with the following:

casperjs test ./testStage.js
like image 167
hexid Avatar answered Jan 03 '23 10:01

hexid


for others who might be an idiot like me, you may want to make sure you are using casperjs in your cmd and not phantomjs

like image 42
stevethecollier Avatar answered Jan 03 '23 12:01

stevethecollier


Had a similar problem. Mine seems to have been caused by the merge of https://github.com/n1k0/casperjs/issues/587 . Going back to 1d1e83ba3c9241623 solved my problems

like image 22
dknaus Avatar answered Jan 03 '23 11:01

dknaus


I just spent the last 2 hours trying to debug this issue:

C:\casperjs>casperjs
CasperError: Can't find module cli

  C:\casperjs\bin\bootstrap.js:262 in patchedRequire
  C:\casperjs\bin\bootstrap.js:375
  C:\casperjs\bin\bootstrap.js:395

I was using PhantomJS 1.9.0 but I found I had to upgrade to 1.9.2 and it started working immediately. I hope this is helpful to someone else.

like image 36
Steve Rukuts Avatar answered Jan 03 '23 12:01

Steve Rukuts