Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nightmare JS not working

I know the title of the question looks very vague! But that's there's to it.

I installed nodejs on my production server, which had phantomjs working properly, then I installed nightmare via npm install nightmare, I can see it in node_modules, I tried the example listed by the developers on github:

var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true })

nightmare
  .goto('http://yahoo.com')
  .type('input[title="Search"]', 'github nightmare')
  .click('#uh-search-button')
  .wait('#main')
  .evaluate(function () {
    return document.querySelector('#main .searchCenterMiddle li a').href
  })
  .end()
  .then(function (result) {
    console.log(result)
  })

Nothing happened, the script did not output anything, I simplified the script to a simple single goto, for a page on my server, the page was never called when I ran the script via node file.js

I have CentOS 6.7, phantomjs 1.1 I also tested it on a fresh CentOS 7 installation with latest version of phantomjs, same thing.

Am I missing some kind of prerequisite or something? How do I debug the issue since node script.js is not giving any output

UPDATE: Apparently the problem is, electron, which is used by nightmare 'instead of phantomjs' requires a graphical enviroment, which is why it fails to run in my enviroment.

like image 592
AL-Kateb Avatar asked Apr 19 '16 13:04

AL-Kateb


People also ask

Why is node js not working?

Make sure the node path is added, if not added it. After doing this restart Visual Studio or open a fresh command prompt. From the command prompt type 'node -v' to echo the node version installed. You can also add the path to node or any other application directly on the command line.

Is Nightmare JS headless Web browser?

Because Nightmare is headless by default and doesn't require a Selenium server like Protractor, it runs very quickly.


3 Answers

New version of Nightmare requires electron, Not PhantomsJs. Make sure electron command is in your $PATH variable.

Install Electron

npm i -g electron-prebuilt

To debug:

DEBUG=nightmare* node script.js

like image 186
hassansin Avatar answered Oct 19 '22 18:10

hassansin


Look at this Dockerfile: https://github.com/aheuermann/docker-electron/blob/master/7/Dockerfile

It s the minimal libs you need. And to start you script:

Xvfb -ac -screen scrn 1280x2000x24 :9.0 &
export DISPLAY=:9.0
DEBUG=* node src/index.js

Electron based app should no more crash

like image 26
Clem Avatar answered Oct 19 '22 19:10

Clem


You can also try to set electron in the background without actually showing any GUI. You check if this works:

var nightmare = Nightmare({ show: false});
like image 1
Jose Bernhardt Avatar answered Oct 19 '22 17:10

Jose Bernhardt