How to use PhantomJS with Node.js on Google App Engine

Daishi Kato
Google Cloud - Community
2 min readJun 2, 2016

There are some posts saying that you can’t use PhantomJS on GAE (Google App Engine), but it’s not true nowadays. This article shows how to configure to use PhantomJS with Node.js.

Disclaimer

Although the following procedure works well as of writing, that doesn’t mean it will work for a long time. I am not even sure if it is recommended. Please understand the risk.

Background

When you code with Node.js, there are many packages on npm, most of them are written in JavaScript, some of them are in native code, and among some of them run in separate processes. PhantomJS is one of them which has to run in a separate process from that of Node.js. PhantomJS has a unique capability of capturing web sites, and there is no alternatives that integrate well with Node.js yet.

GAE is a PaaS and is typically limited to run a single process. However, the relatively recent Flexible Environment (formally called “Managed VMs”) is more flexible. It simply allows to run anything in a Docker container in Google Compute Engine VMs.

Problem

There is a nice PhantomJS package for Node.js called phantomjs-prebuilt. If you look at its dependents, you see so many packages depend on it. Now, this package works well for the most cases, but there is an important note in the document.

There is no requirement to install Qt, WebKit, or any other libraries. It however still relies on Fontconfig (the package fontconfig or libfontconfig, depending on the distribution).

The issue is that this fontconfig package is not installed in a container for Node.js in GAE.

Solution

One possible solution would be to use the Custom Runtimes and write your own Dockerfile to create a container.

However, I wanted something much simpler, and found a solution. All you need is to add a line in your package.json file.

{
"scripts": {
"preinstall": "apt-get update && apt-get -y
install libfontconfig1"
},
"dependencies": {
"phantomjs-prebuilt": "^2.1.7"
}
}

Please note that it only shows the part of the package.json file. This works like a charm.

Some final thoughts

Not much to say here, but again I am not sure if this is the best way. Ideally, it would be much better to have a pure Node.js package that has similar functionalities of PhantomJS. For the last comment, I would note that you might need some tweaks so that the preinstall script only runs on GAE.

--

--