I need to use colors (or similar npm module, suggestions are welcome) with phantomjs. This is the code that I have:
var page = require('webpage').create();
var url = require('system').args[1];
var fs = require('fs');
var colors = require('colors');
console.log(">>In Phantomjs Process...".yellow);
page.open(url, function (status) {
if (status !== 'success') {
console.log(">>Error Opening Webpage.".red);
phantom.exit();
}
else {
console.log(">>Page Loaded Successfully...".yellow);
window.setTimeout(function () {
fs.write('index.txt', page.content, 'w');
phantom.exit();
}, 10000);
}
});
This code seems to run fine on Windows 7, x86 machine with phantomjs version 1.9.7,
$ phantomjs fetchDOM.js "http://www.google.com"
>>In Phantom Process...
>>Page Loaded Successfully...
but on Ubuntu 13.04 x86 machine with phantomjs version 1.9.7,
$ phantomjs fetchDOM.js "http://www.google.com"
Unknown module colors for require()
Why is this happening? Is there any workaround for this? Thank you in advance.
colors module documentation can be found here: https://www.npmjs.org/package/colors
EDIT: If I replace var colors = require('colors');
with phantom.injectJs('colors');
, the error disappears, but the output is not as expected.
$ phantomjs fetchDOM.js "http://www.google.com"
undefined
undefined