Running PhantomJS on Modulus

Running PhantomJS on Modulus

PhantomJS is a very cool piece of technology. It’s a headless browser that lets you programmatically interact with websites. It’s used a lot for screen capturing, automation, and performance testing. Since going live we’ve been asked a lot about PhantomJS support, and we thought it was finally time to talk about how to use PhantomJS in your Modulus projects.

PhantomJS is not installed natively in every servo, however it’s incredibly easy to get installed and running by using the phantomjs NPM module. As part of the install process, this module downloads the correct PhantomJS binary and copies it to its node_modules folder.

Here’s an example PhantomJS script we’ll be running for this article. It takes a screenshot of the Modulus homepage and saves it to the public folder.

var page = require('webpage').create();
page.open('https://modulus.io', function () {

Using the phantomjs NPM Module

The phantomjs module installs everything that’s needed to immediately get started with Phantom. All you have to do is put the dependency in your package.json file. Below is the code that will run our Modulus screen capturing script.

var phantomjs = require('phantomjs'),
    path = require('path'),
    childProcess = require('child_process');

var binPath = phantomjs.path;

var childArgs = [
  path.join(__dirname, 'phantom-script.js'),
  'some other argument (passed to phantomjs script)'

childProcess.execFile(binPath, childArgs, 
  function(err, stdout, stderr) {

Using other Modules

Not everyone will want to use the phantomjs module listed above, and that’s fine. You should still include it as a dependency just to get the PhantomJS binary installed correctly. Once the binary is installed, you can use whatever module you like. The binary will be located at the following path:


This path is also available by looking at:

var phantomBinaryPath = require('phantomjs').path;

Most modules out there will let you specify where the PhantomJS binary is. Simply specify this location and you’ll be good to go.

Updating PATH

Some modules require the PhantomJS binary to be available in the path. You can easily update the PATH environment variable in your Node application with the following lines:

process.env.PATH += ':' + path.dirname(phantomjs.path);

The PhantomJS binary location is now in the PATH and can be executed directly. For example:

child_process.exec('phantomjs --version', 
  function(err, stdout, stderr) {
    console.log(stdout); // 1.9.0

Meteor Users

Some Meteor packages, like spiderable, require PhantomJS to be installed. Here are the steps Meteor users will follow in order to use PhantomJS in their applications.

  1. At the start of your application, update the PATH environment variable by following the steps in the previous section.
  2. Demeteorize your application.
  3. Manually add phantomjs dependency to your package.json file. Your package.json file will be located in the root of your .demeteorized folder.
  4. Deploy your application to Modulus.

What is Xervo?

Xervo makes deploying applications in the public cloud or your own data center easy. Node.js, PHP, Java, Python, Nginx, and MongoDB supported. Full Docker support included in Enterprise version. It’s free to get started.

Share This Article

comments powered by Disqus