你没有安装'phantomjs'

问题描述 投票:4回答:4

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

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

node.js path phantomjs
4个回答
3
投票

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

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

var phantom = require('phantom');

更多,添加utnas如何:

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

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


4
投票

我在macOS上也遇到了这个错误,所以这个命令就行了。

brew install phantomjs

编辑:phantomjs被转移到Cask。所以在2019年运行:

brew cask install phantomjs

1
投票

在这种情况下接受的答案并不是真正的解决方案。错误消息You don't have 'phantomjs' installedphantomjs-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二进制文件的目录的完整路径。


0
投票

但是,如果你在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
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.