Yes, you can run Nightmare, which is a high-level browser automation library for Node.js, on a headless server. Initially, Nightmare was designed to work with Electron, which requires a graphical environment. However, with the introduction of headless mode in Electron and Chromium, it is now possible to run Nightmare on a server without a GUI.
To run Nightmare in headless mode, you need to ensure that you have the necessary dependencies installed on your server. Since Nightmare uses Electron, you must have a compatible version of Electron that supports headless execution.
Here's a basic example of running Nightmare in headless mode using Node.js:
const Nightmare = require('nightmare');
// Enable the headless mode by passing { show: false }
let nightmare = Nightmare({ show: false });
nightmare
.goto('https://example.com')
.evaluate(() => {
return document.title;
})
.end()
.then(title => {
console.log(title);
})
.catch(error => {
console.error('Error:', error);
});
To run Nightmare on a headless server, you'll need to set up an X virtual framebuffer (Xvfb) if you're using a Unix-based system without a GUI. This allows Electron to run in a virtual environment that simulates a display. Here's how to install and use Xvfb:
- Install Xvfb and other dependencies:
sudo apt-get update
sudo apt-get install -y xvfb x11-xkb-utils xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic x11-apps
- Start Xvfb before running your Nightmare script:
Xvfb -ac -screen scrn 1280x2000x24 :9.0 &
export DISPLAY=:9.0
- Run your Nightmare script with Node.js:
node your_nightmare_script.js
In modern setups, you might not need Xvfb if you are using a version of Electron that supports true headless mode. Make sure to check the version of Electron that comes with Nightmare and update it if necessary to take advantage of the headless feature without Xvfb.
Lastly, keep in mind that web scraping can be against the terms of service of many websites, and you should always respect the rules and legal constraints of the target website when performing web scraping activities.