Node.js 你没有';我没有';幻影';安装

Node.js 你没有';我没有';幻影';安装,node.js,path,phantomjs,Node.js,Path,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

我确实安装了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中,我将无法定义

但在油灰中,如果我:

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

我安装错地方了吗?

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

加载本地模块会阻止应用程序定位可运行存储箱:

var phantom = require('phantom');
另外,添加utnas注释:

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


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

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

在这种情况下,公认的答案并不是真正的解决方案。错误消息
您没有安装“phantomjs”
是来自模块的内部错误。我自己也遇到了这个错误,我成功地解决了这个问题:

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
    }
});
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()
方法。
path
选项应该是包含phantomjs二进制文件的目录的完整路径。

我在macOS上也遇到了这个错误,所以这个命令成功了

brew install phantomjs
编辑: 幻影被移到木桶里。因此,在2019年:

brew cask install phantomjs

它是否在PATH变量中?您是如何安装它的?npm安装-g phantomjs?节点模块很好,但phantomjs bin的东西不是remove var modules='/home/engine/node_modules/';这是没有用的。节点知道在哪里可以找到模块