How to Run Nightmare JS On Remote Linux Server

Nightmare js is a browser automation tool. It works perfectly on local machine and even has an option to view the browser in action by passing { show: true } option when initializing Nightmare. On remote linux machine, however, it won’t work without installing and including an extra package xvfb.

Below is how you can add it to your app for nightmare to work seamlessly:

const Nightmare = require('nightmare');
const Xvfb = require('xvfb');
let xvfb = new Xvfb();

try {
  xvfb.startSync();
}
catch (e) {
  console.log(e);
}

// now anywhere in the code you can create and use a nightmare instance
const nightmare = Nightmare({});
  nightmare
    .goto('https://www.techighness.com')
    //...

Note that a machine that doesn’t have xvfb installed, it’ll just throw an error and still start the app, but nightmare code won’t work.

See also