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

casperjs, require

Solution

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

Problem

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 ```

Original source