Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a node module from casperjs

Is it possible to install a node module, installed via npm, and then require it from a casperjs script?

(I see lots of posts and tools for running casper or phantom from inside node.js, but that is not what I'm trying to do.)

The casperjs docs seem to say it is possible, but only show with hand-written toy modules that don't really do anything. The real-world module I'm trying to install is imap, but at this point I cannot get any module to work, even built-in ones like net. Simple example:

npm install imap
echo "var test = require('imap');" > test.js
casperjs test.js

Gives me:

CasperError: Can't find module imap

/usr/local/src/casperjs/bin/bootstrap.js:263 in patchedRequire
test.js:1

(I can see the imap module from npm ls, and I can use it fine from a node.js script.)

Or alternatively with a built-in module:

echo "var test = require('net');" > test.js
casperjs test.js

Complains "Can't find module net"


I have npm --version of 1.4.14 and nodejs --version of v0.10.29. Are either of those too old, I wonder? (Casper is 1.1.0-beta, and Phantom is 1.9.7, both of which are the latest versions.)

like image 889
Darren Cook Avatar asked Jun 24 '14 13:06

Darren Cook


1 Answers

Here an example with the colors module :

var colors = require('colors');


 casper.test.begin('\n*Colors module*\n', function suite(test) {
    casper.start()
    .thenOpen('https://www.google.fr/', function() {
        console.log("test require\n".zebra);
        console.log("test require\n".rainbow);
        console.log("test require\n".red.underline.bold);
      })
    .run(function() {
            test.done();
    });
});
  • node-modules
    • colors
  • testnode.js

casperjs test testnode.js

output :

casper + colors module -npm-

It seems it's not as simple when the required module has dependencies.

like image 54
Fanch Avatar answered Oct 17 '22 18:10

Fanch