Nightmare.js在Ubuntu Linux云服务器上无法正常工作

时间:2016-01-14 16:44:20

标签: javascript linux node.js ubuntu nightmare

我似乎无法让nightmare.js在Ubuntu Linux 14.04服务器上工作[通过DigitalOcean]。

我安装了PhantomJS(1.9.8)和Node(4.2.4),据我所知,它们运行良好。

例如,当我运行时:

phantomjs loadspeed.js http://www.yahoo.com

with loadspeed.js包含:

"use strict";
var page = require('webpage').create(),
    system = require('system'),
    t, address;

if (system.args.length === 1) {
    console.log('Usage: loadspeed.js <some URL>');
    phantom.exit(1);
} else {
    t = Date.now();
    address = system.args[1];
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('FAIL to load the address');
        } else {
            t = Date.now() - t;
            console.log('Page title is ' + page.evaluate(function () {
                return document.title;
            }));
            console.log('Loading time ' + t + ' msec');
        }
        phantom.exit();
    });
}

我得到以下输出:

Page title is Yahoo
Loading time 700 msec

然而,当我尝试运行一个简单的噩梦时:

node --harmony hello_nightmare.js

包含以下内容的hello_nightmare.js:

var Nightmare = require('nightmare');

var google = new Nightmare()
  .goto('http://google.com')
  .wait()
  .run(function(err, nightmare) {
    if (err) return console.log(err);
    console.log('Done!');
  });

我得不到任何输出;感觉就像我在命令行上按下'Enter'一样。

我也在噩梦github网站上尝试了这个例子:

npm install nightmare vo
node --harmony hello_nightmare_main.js

包含以下内容的hello_nightmare_main.js:

var Nightmare = require('nightmare');
var vo = require('vo');

vo(function* () {
  var nightmare = Nightmare({ show: true });
  var link = yield nightmare
    .goto('http://yahoo.com')
    .type('input[title="Search"]', 'github nightmare')
    .click('.searchsubmit')
    .wait('.ac-21th')
    .evaluate(function () {
      return document.getElementsByClassName('ac-21th')[0].href;
    });
  yield nightmare.end();
  return link;
})(function (err, result) {
  if (err) return console.log(err);
  console.log(result);
});

它仍然不起作用。

如何解决这场噩梦?

4 个答案:

答案 0 :(得分:8)

您的问题很可能是由您描述的 https://github.com/segmentio/nightmare/issues/224

梦魇使用需要X显示的Electron;由于您的服务器没有显示器,您可以使用Xvfb提供虚拟显示器。安装xvfb,然后运行

xvfb-run node --harmony hello_nightmare.js

答案 1 :(得分:8)

我只是为后人发帖。

下面是在干净的Ubuntu Linux机器上安装带有节点(4.2.4)的nightmarejs的bash脚本。我在运行14.04的DigitalOcean液滴上测试了这个。

apt-get -y update
apt-get -y upgrade
apt-get -y --force-yes install make unzip g++ libssl-dev git xvfb x11-xkb-utils xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic x11-apps clang libdbus-1-dev libgtk2.0-dev libnotify-dev libgnome-keyring-dev libgconf2-dev libasound2-dev libcap-dev libcups2-dev libxtst-dev libxss1 libnss3-dev gcc-multilib g++-multilib
mkdir src
cd src
wget https://nodejs.org/dist/v4.2.4/node-v4.2.4.tar.gz
tar xzf node-v4.2.4.tar.gz
cd node-v4.2.4
./configure
make -j2
make install
cd ..
mkdir nightmarejs
cd nightmarejs
npm -f init
npm install --save nightmare vo

然后你只需创建.js文件(例如hello_nightmare.js)(在安装了nightmarejs的同一目录下),然后使用下面的命令运行它(如@ yoz&#39; s中已经提到的那样):< / p>

xvfb-run node --harmony hello_nightmare.js

我希望这会有所帮助。

答案 2 :(得分:5)

由于电子需要X显示,您需要安装以下所有包

sudo apt-get install -y xvfb x11-xkb-utils xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic x11-apps clang libdbus-1-dev libgtk2.0-dev libnotify-dev libgnome-keyring-dev libgconf2-dev libasound2-dev libcap-dev libcups2-dev libxtst-dev libxss1 libnss3-dev gcc-multilib g++-multilib

aws ec2的ubuntu服务器中测试过它

然后运行你的脚本:

xvfb-run node --harmony script.js

答案 3 :(得分:0)

Nightmare.js 使用 Electron 浏览器并需要 X 服务器。安装 xvfb 及其依赖项,以便您可以在没有显示硬件的情况下运行图形应用程序:

sudo apt-get install -y xvfb \
x11-xkb-utils \
xfonts-100dpi \
xfonts-75dpi \
xfonts-scalable \
xfonts-cyrillic \
x11-apps \
clang libdbus-1-dev \
libgtk2.0-dev libnotify-dev \
libgconf2-dev \
libasound2-dev libcap-dev \
libcups2-dev libxtst-dev \
libxss1 libnss3-dev \
gcc-multilib g++-multilib

创建 nightmare.js 文件并添加以下内容:

const Nightmare = require("nightmare");    
const outputFile = "test.png";

const nightmare = Nightmare({ show: true })

nightmare
  .goto('https://duckduckgo.com')
  .type('#search_form_input_homepage', 'github nightmare')
  .click('#search_button_homepage')
  .wait('#r1-0 a.result__a')
  .evaluate(() => document.querySelector('#r1-0 a.result__a').href)
  .end()
  .then(res => {
    console.log(res)
  })
  .catch(error => {
    console.error('Search failed:', error)
  })

运行脚本:

$ xvfb-run node --harmony nightmare.js
// output: https://github.com/segmentio/nightmare