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

BannerMan picture BannerMan · Jan 14, 2016 · Viewed 7.8k times · Source

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?

Answer

yoz picture yoz · Jan 14, 2016

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