3

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
  • Cannot reproduce: Did you actually install colors through `npm install colors` in the folder where your script is located? It does not work globally, because phantomjs does not use the npm repository. – Artjom B. Jul 11 '14 at 17:29
  • Yes, I have done it already. –  Jul 11 '14 at 17:33
  • Have you tried requiring it directly like `require('./node_modules/colors/colors.js')`? – Artjom B. Jul 11 '14 at 17:41
  • No. Getting same error back `Unknown module ./node_modules/colors/colors.js for require()` –  Jul 11 '14 at 17:48
  • See also http://stackoverflow.com/a/24388906/841830 (It worked for the answerer, but did not work for me on Linux, so I'm curious now if he was using Windows, and the problem is there... but I'm guessing the problem is something to do with how I have npm setup.) – Darren Cook Jul 12 '14 at 16:52
  • If problem was in npm, I wouldn't have been able to use colors module through node.js, but it is not the case. module works fine with node.js. The issue is phontomjs specific. –  Jul 12 '14 at 17:52

1 Answers1

0

The issue is that process API is not available in PhantomJS. I fixed it by modifying support-colors.js present in /colors/lib/system/supports-colors.js

/*
The MIT License (MIT)

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/
var system = require('system');
var argv = system.args;
var env = system.env;
var os = system.os;

module.exports = (function () {
  if (argv.indexOf('--no-color') !== -1 ||
    argv.indexOf('--color=false') !== -1) {
    return false;
  }

  if (argv.indexOf('--color') !== -1 ||
    argv.indexOf('--color=true') !== -1 ||
    argv.indexOf('--color=always') !== -1) {
    return true;
  }

  //if (process.stdout && !process.stdout.isTTY) {
  //  return false;
  //}

  if (os.architecture === 'win32' && os.name === 'windows') {
    return true;
  }

  if ('COLORTERM' in env) {
    return true;
  }

  if (env['TERM'] === 'dumb') {
    return false;
  }

  if (/^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(env['TERM'])) {
    return true;
  }

  return false;
})();
xboz
  • 174
  • 1
  • 8