你没有安装“phantomjs”

我已经安装了PhantomJS,但是当我运行我的Node.js代码时,出现错误(您没有安装“phantomjs”):

var modules = '/home/engine/node_modules/'; var path = require('path'); var childProcess = require('child_process'); var phantom = require(modules+'phantom'); var binPath = phantom.path; phantom.create(function(browser){ // Error happens here I think because the module is found // browser.createPage(function (page){}); }); 

如果在console.log binPath我得到未定义。

但是在PuTTY中,如果我:

 cd ~/phantomjs/ [root@engine phantomjs]# bin/phantomjs phantomjs> 

我把它安装在错误的地方了吗?

您需要加载您的全球PhantomJS模块,而不是本地。

加载本地模块会阻止应用程序查找可运行bin:

 var phantom = require('phantom'); 

另外,添加utnas评论:

删除var modules ='/ home / engine / node_modules /'; 。 这没有用。 Node.js知道在哪里可以find模块。

将这两个部分的答案混合在一个逻辑规则中,Node.js将始终首先从全局安装的模块(如果存在的话)加载模块。 你强制它加载本地的,并阻止它find垃圾箱。

我也在我的macOS得到这个错误,所以这个命令做了伎俩。

 brew install phantomjs 

在这种情况下被接受的答案并不是真正的解决scheme。 错误消息You don't have 'phantomjs' installed是来自phantomjs-node模块的内部错误。 我自己遇到了这个错误,我设法解决这个问题:

 var phantom = require('phantom'); var options = { path: '/usr/local/bin/' }; phantom.create(function (ph) { ph.createPage(function (page) { page.open("http://www.google.com", function (status) { console.log("opened google? ", status); page.evaluate(function () { return document.title; }, function (result) { console.log('Page title is ' + result); ph.exit(); }); }); }); }, options); 

注意传递给phantom.create()方法的optionspath选项应该是包含phantomjs二进制文件的目录的完整path。

但是如果你在Windows上,你可以尝试一下这样的事情:

 var phantom = require('phantom'); phantom.create(function (ph) { ph.createPage(function (page) { page.open("http://www.google.com", function (status) { console.log("opened google? ", status); page.evaluate(function () { return document.title; }, function (result) { console.log('Page title is ' + result); ph.exit(); }); }); }); }, { dnodeOpts: { weak: false } });