Using NightmareJS/Electron on Amazon Linux

We recently changed from PhantomJS to NightmareJS to improve quality of PNG files generated on MockingBot, and here's how we get NightmareJS/Electron run on Amazon Linux.

0. Install nightmare

$ npm install --save nightmare

1. Check dependencies

As Amazon Linux don't provide desktop environment, we need to resolve the dependencies of prebuilt electron manually.

$ cd node_modules/nightmare/node_modules/electron-prebuilt/dist
$ ldd electron | grep ‘not found' => not found => not found => not found => not found => not found => not found => not found => not found => not found => not found => not found

Use ldd, we can see there're 11 libraries we miss. Pango, Xrandr, Xcursor and Xcomposite are included in Amazon Linux, let's install those with yum:

$ sudo yum install pango pango-devel libXrandr libXrandr-devel libXcursor libXcursor-devel libXcomposite libXcomposite-devel

and we can find atk from CentOS 6:

$ sudo rpm -ivh
$ sudo rpm -ivh

Now comes the hard part.

2. GConf

GConf is depended by GTK+, so we need to compile it first:

# Install GConf dependencies
$ sudo yum install libIDL libIDL-devel gtk-doc dbus dbus-devel dbus-glib dbus-glib-devel intltool indent
$ sudo rpm -ivh
$ sudo rpm -ivh

# Compile GConf
$ cd /tmp
$ wget
$ tar -jxvf GConf-2.32.4.tar.bz2
$ cd GConf-2.32.4
$ ./configure && make
$ sudo make install

3. GTK+

It took me some time to find a capable version of GTK+, and finally I found 2.24.0 work well will current release of Amazon Linux.

# Compile gdk-pixbuf
$ cd /tmp
$ wget
$ tar -jxvf gdk-pixbuf-2.24.0.tar.bz2
$ cd gdk-pixbuf-2.24.0
$ ./configure
$ make
$ sudo make install

# Compile GTK+
$ wget
$ tar -jxvf gtk+-2.24.0.tar.bz2
$ cd gtk+-2.24.0
$ PKG_CONFIG_PATH=/usr/local/lib/pkgconfig ./configure
$ make
$ sudo make install

4. Linking

Now we've got all libraries we needed, let's link them to prebuilt electron:

$ cd ~/node_modules/electron-prebuilt/dist
$ ln -PL /usr/local/lib/
$ ln -PL /usr/local/lib/
$ ln -PL /usr/local/lib/
$ ln -PL /usr/local/lib/

Now run ldd again, and you should get empty output:

$ ldd electron | grep 'not found'

5. Run

To run Electron on Amazon Linux, here's the last step we needed: setup X.

# Install X
$ sudo yum install xorg-x11-server-Xorg xterm
# Install X drivers
$ sudo yum install xorg-x11-drv-vesa xorg-x11-drv-evdev xorg-x11-drv-evdev-devel
# Install xvfb
$ sudo yum install Xvfb

Okay, finally, let's check our work with an example from NightmareJS homepage:

$ xvfb-run -a --server-args="-screen 0 1024x768x24" node cnn.js
Breaking News, Daily News and Videos -

# cnn.js
var Nightmare = require('nightmare');
var vo = require('vo');

vo(run)(function(err, result) {
  if (err) throw err;

function *run() {
  var nightmare = Nightmare();
  var title = yield nightmare
    .evaluate(function() {
      return document.title;
  yield nightmare.end();

Cool, it works!
Comments 1