phantomjslibxml-js

Error: Cannot find module 'libxmljs'


I am trying to parse xml using phantomjs for the following file, documentpreviewer1.js

var webPage = require('webpage');
var page = webPage.create();

var url = "http://xxx/sitemap.xml";

page.open(url, function(status){
    if(status != 'success'){
                console.log('Unable to access cfc');
    }
    else
    {
                var xml = page.content;
                var libxmljs = require("libxmljs");
                var xmlDoc = libxmljs.parseXml(xml);

                var url1 = xmlDoc.get('//urlset/url[0]/loc');
                console.log(url1);
    }
});

when I run the above code, I get the following error

cmd sudo phantomjs documentpreivewer1.js

Error: Cannot find module 'libxmljs'

  phantomjs://bootstrap.js:289
  phantomjs://bootstrap.js:254 in require
  documentpreivewer1.js:13
  :/modules/webpage.js:281

Solution

  • libxmljs is a node.js module. Although phantomjs can be installed through npm (doesn't need to be) it is not a node.js module. It doesn't share any built-in module with node.js (fs seems the same, but is not equal to node.js fs).

    You can use some node.js modules in phantomjs (See Use a node module from casperjs for a related question), but it doesn't seem like you could use libxmljs in phantomjs, because it depends on node-bindings which uses fs and path modules. You will have to change the implementation so that all dependencies can be expressed with phantomjs capabilities.

    An alternative might be to use phantom-node or spookyjs for a casperjs node.js module.