How to run nightmare.js on google appengine for node.js

There is a famous problem with a nightmare and an electron that does not work on headless Linux servers. Official electronic documents suggest using xvfb to fake the display. They suggest using this .yml file for travis.

  addons:
   apt:
     packages:
       - xvfb

 install:
   - export DISPLAY = ': 99.0'
   - Xvfb: 99 -screen 0 1024x768x24> / dev / null 2> & 1 &

Question

How can I use the above code snippet in google appengine app.yaml file for node.js. I tried to use it as is, but glcoud throws an error, which addon is an invalid command. The gap in official documents does not have such a team.

Any suggestions on how we can run the nightmare and electron in the google appengine for node.js ..?

+7
google-app-engine yaml nightmare electron
source share
2 answers

There are two parts to this question:

  • Running chrome (which electron and, in turn, a nightmare "uses") without a head on linux.
  • Install / use xvfb to run chrome on the application engine.

Part 1)

You need xvfb.

Xvfb (Virtual Framebuffer) is just a program that from the wiki: "is a display server that implements the X11 display server protocol. Unlike other display servers, Xvfb performs all the graphic operations in memory without displaying any output to the screen."

This is what you need to start the browser without displaying the screen.

First install all xvfb related packages to run on Linux.

apt-get install -y \ xvfb \ x11-xkb-utils \ xfonts-100dpi \ xfonts-75dpi \ xfonts-scalable \ xfonts-cyrillic \ x11-apps \ clang \ libdbus-1-dev \ libgtk2.0-dev \ libnotify-dev \ libgnome-keyring-dev \ libgconf2-dev \ libasound2-dev \ libcap-dev \ libcups2-dev \ libxtst-dev \ libxss1 \ libnss3-dev \ gcc-multilib \ g++-multilib

So, when installing xvfb, you need to create an xvfb virtual screen and export an environment variable called DISPLAY that points to it. Chromium in Electron will automatically search for $ DISPLAY.

The above can be done more easily. Here are two options:

  • Calling a program using linux cli (ignore xvfb warnings if the nightmare script is working fine):

    • xvfb-run -a node main.js Or...

    • If you use visualization-related features such as taking screenshots: xvfb-run -a --server-args="-screen 0 1280x1028x24 -ac +extension GLX +extension RANDR +render" node app.js Google xvfb options to adapt to your taste.

  • Programmatically: using the xvfb npm package

From now on, you should be able to run a nightmare on Linux.

Part 2)

Nodejs in the application engine runs through a flexible environment. Sense through docker containers.

From the GAE nodejs runtime: β€œIf your application needs additional dependencies at the operating system level, you will need to use a custom runtime based on this runtime to install the appropriate packages.”

Docker is a whole separate topic, but in order to do this using the application, you have two options, as far as I know:

In any case, basically, you will need to install the packages related to xvfb that define them in the docker file, and this should do the trick.

Good luck

Important notes:

  • The apt-get packages mentioned above depend on the availability of the Linux distribution (the code above works on ubuntu and debian). For example, with the specified set of packages and during this message it will work with a flexible GAE environment, since it is based on debian jessie and will not work on linux alpine.

  • Chromium requires a minimal dev / shm distribution to work. For example, 5mb is fixed on the hero, and there is no way to change it. After a few nightmares, chrome will work. Thus, chrome will not work on any of the heroin speakers of any size. The docker is set to 64 MB, so depending on the complexity of your script, you will do whatever you need, or you need to adjust it. On simple Linux installations, dev / shm is usually installed in half the total available memory. Thus, in a 512 MB environment, dev / shm will be set to 256 MB, and the nightmare will most likely work fine.

+12
source share

Thanks @pickmed for his thorough answer! This helped me to get started with using xvfb in this context. ( fooobar.com/questions/838982 / ... )

I use Nightmare to create a PDF file from an endpoint. My local developer works on OSX, and I got into this problem trying to get it to work in the Google App Engine. At first, I started working with a rickety answer, and since then I figured out another way that avoids the custom Docker / runtime file. I thought I would share it here.

I do not use a custom Docker file and let gcloud generate it for me during deployment. The yaml file uses runtime: nodejs . For my simple use of Nightmare, I can add a preinstall script to my package.json and update the start script. This is all I need for the nightmare to work on GAE. Here are the relevant lines from my package.json:

 { "scripts": { "preinstall": "apt-get update && apt-get install -y libgtk2.0-0 libgconf-2-4 libasound2 libxtst6 libxss1 libnss3 xvfb", "start": "xvfb-run -a node build/server/index", ... }, ... } 

I pulled a simplified set of apt-get installed packages from the otaviomedeiros comment: https://github.com/segmentio/nightmare/issues/224#issuecomment-225887320

I got an idea from Daishi Kato for a useful article: https://medium.com/google-cloud/how-to-use-phantomjs-with-node-js-on-google-app-engine-6f7feaea551#.6eoyvpn93 , and this one Disclaimer included in the article:

Despite the fact that the following procedure works well, as it is written, this does not mean that it will work for a long time. I'm not even sure if this is recommended. Pay attention to the risk.

So, take this for what it is, and hopefully it helps someone!

+2
source share

All Articles