Error: Cannot find module 'libxmljs'

拜拜、爱过 提交于 2019-12-11 08:15:55

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/24602466/error-cannot-find-module-libxmljs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!