错误:找不到模块"libxmljs" [英] Error: Cannot find module 'libxmljs'
问题描述
我正在尝试使用phantomjs解析以下文件documentpreviewer1.js
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
推荐答案
libxmljs是node.js模块.尽管phantomjs可以通过npm安装(不需要安装),但它不是node.js模块.它不与node.js共享任何内置模块( fs
似乎相同,但不等于node.js fs).
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).
您可以在phantomjs中使用一些node.js模块(有关相关问题,请参见使用casperjs中的节点模块) ,但是您似乎不能在phantomjs中使用libxmljs,因为它依赖于使用fs
和path
模块的节点绑定.您将不得不更改实现,以便可以使用phantomjs功能来表达所有依赖关系.
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.
这篇关于错误:找不到模块"libxmljs"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!