简体   繁体   中英

Nightmare.js not working as expected on Ubuntu Linux cloud server

I can't seem to get nightmare.js to work on an Ubuntu Linux 14.04 server [via DigitalOcean].

I've installed PhantomJS (1.9.8) and Node (4.2.4), and they are working well as far as I can tell.

For example, when I run this:

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

with loadspeed.js containing this:

"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();
    });
}

I get the following output:

Page title is Yahoo
Loading time 700 msec

However, when I try running a simple nightmare:

node --harmony hello_nightmare.js

with hello_nightmare.js containing this:

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!');
  });

I get no output whatsoever; it feels like I just pressed 'Enter' on the command line.

I also tried the example on the nightmare github site:

npm install nightmare vo
node --harmony hello_nightmare_main.js

with hello_nightmare_main.js containing this:

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);
});

And it still doesn't work.

How do I fix this nightmare?

Your issue is most likely described by https://github.com/segmentio/nightmare/issues/224

Nightmare uses Electron which requires an X display; since your server doesn't have a display, you can use Xvfb to provide a virtual one. Install xvfb, and run

xvfb-run node --harmony hello_nightmare.js

I'm just posting this for posterity.

Below is the bash script to install nightmarejs with node (4.2.4) on a clean Ubuntu Linux machine. I've tested this on a DigitalOcean droplet running 14.04.

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

Then you simply create the.js file (eg hello_nightmare.js) (in the same directory where nightmarejs is installed) and then run it using the command below (as already mentioned in @yoz's answer):

xvfb-run node --harmony hello_nightmare.js

I hope this helps.

Since electron requires X display you need to install all the following packages

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

Tested in ubuntu server in aws ec2 and it worked

then run your script:

xvfb-run node --harmony script.js

Nightmare.js uses the Electron browser and requires an X server. Install xvfb and its dependencies so that you can run graphical applications without display hardware:

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

Create nightmare.js file and add the following:

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)
  })

run the script:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM