【问题标题】:You don't have 'phantomjs' installed您没有安装“phantomjs”
【发布时间】:2015-04-18 11:13:03
【问题描述】:

我确实安装了 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>

我是不是安装错地方了?

【问题讨论】:

  • 是否在PATH变量中?
  • 你是怎么安装的? npm install -g phantomjs ?
  • node 模块找到了,但是 phantomjs bin 的东西不是

标签: node.js path phantomjs


【解决方案1】:

我在我的 macOS 上也遇到了这个错误,所以这个命令可以解决问题。

brew install phantomjs

编辑: phantomjs 已移至 Cask。所以在 2019 年运行:

brew cask install phantomjs

【讨论】:

    【解决方案2】:

    在这种情况下,接受的答案并不是真正的解决方案。错误消息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);
    

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

    【讨论】:

      【解决方案3】:

      但如果你在 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
          }
      });
      

      【讨论】:

        【解决方案4】:

        你需要加载你的全局 PhantomJS 模块而不是本地的。

        加载本地模块会阻止您的应用程序定位可运行的 bin:

        var phantom = require('phantom');
        

        另外,添加utnas评论:

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

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

        【讨论】:

        • 删除 var modules = '/home/engine/node_modules/';没用。 Node 知道在哪里可以找到模块
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-05-31
        • 1970-01-01
        • 1970-01-01
        • 2017-12-22
        • 2016-08-27
        • 2015-09-17
        • 1970-01-01
        相关资源
        最近更新 更多