Catalog npm
Back to packageRELEASE DETAILS

0.0.1

npm

352-phantom

PhantomJS wrapper for Node

phantomjs
Published9 Dec 2014
Direct dependencies12
Transitive dependenciesNot calculated

README

Current package documentation

PhantomJS bridge for NodeJS Build Status

"It sure would be neat if PhantomJS was a NodeJS module", I hear you say. Well, wait no longer! This node module implements a nauseously clever bridge between Phantom and Node, so that you can use all your favourite PhantomJS functions without leaving NPM behind and living in a cave.

Installation

First, make sure PhantomJS is installed. This module expects the phantomjs binary to be in PATH somewhere. In other words, type this:

$ phantomjs

If that works, so will phantomjs-node. It's only been tested with PhantomJS 1.3, and almost certainly doesn't work with anything older.

Install it like this:

npm install phantom

For a brief introduction continue reading, otherwise go to the Wiki page for more information!

How do I use it?

Use it like this in Coffeescript:

phantom = require 'phantom'

phantom.create (ph) ->
  ph.createPage (page) ->
    page.open "http://www.google.com", (status) ->
      console.log "opened google? ", status
      page.evaluate (-> document.title), (result) ->
        console.log 'Page title is ' + result
        ph.exit()

In Javascript:

var phantom = require('phantom');

phantom.create(function (ph) {
  ph.createPage(function (page) {
    page.open("http://www.google.com", function (status) {
      console.log("opened google? ", status);
      page.evaluate(function () { return document.title; }, function (result) {
        console.log('Page title is ' + result);
        ph.exit();
      });
    });
  });
});

Use it in Windows

It would use dnode with weak module by default. It means that you need to setup node-gyp with Microsoft VS2010 or VS2012, which is a huge installation on Windows.

dnodeOpts property could help you to control dnode settings, so you could disable weak by setting it false to avoid that complicated installations.

var phantom = require('phantom');

phantom.create(function (ph) {
  ph.createPage(function (page) {
    /* the page actions */
  });
}, {
  dnodeOpts: {
    weak: false
  }
});

Use it in restricted enviroments

Some enviroments (eg. OpenShift) have special requirements that are difficult or impossible to change, especifficaly: hostname/ip and port restrictions for the internal communication server and path for the phantomjs binary.

By default, the hostname/ip used for this will be localhost, the port will be port 0 and the phantomjs binary is going to be assumed to be in the PATH enviroment variable, but you can use specific configurations using an options object like this:

var options = {
  port: 16000,
  hostname: "192.168.1.3",
  path: "/phantom_path/"
}

phantom.create(function, options);

Functionality details

You can use all the methods listed on the PhantomJS API page

Due to the async nature of the bridge, some things have changed, though:

  • Return values (ie, of page.evaluate) are returned in a callback instead
  • page.render() takes a callback so you can tell when it's done writing the file
  • Properties can't be get/set directly, instead use page.get('version', callback) or page.set('viewportSize', {width:640,height:480}), etc. Nested objects can be accessed by including dots in keys, such as page.set('settings.loadImages', false)
  • Callbacks can't be set directly, instead use page.set('callbackName', callback), e.g. page.set('onLoadFinished', function(success) {})
  • onResourceRequested takes a function that executes in the scope of phantom which has access to request.abort(), request.changeUrl(url), and request.setHeader(key,value). The second argument is the callback which can execute in the scope of your code, with access to just the requestData. e.g.
page.onResourceRequested(
	function(requestData, request) { request.abort(); },
	function(requestData) { console.log(requestData.url) }
);

ph.createPage() makes new PhantomJS WebPage objects, so use that if you want to open lots of webpages. You can also make multiple phantomjs processes by calling phantom.create('flags', { port: someDiffNumber}) multiple times, so if you need that for some crazy reason, knock yourself out!

Also, you can set exit callback, which would be invoked after phantom.exit() or after phantom process crash:

phantom.create('flags', { port: 8080, onExit: exitCallback})

You can also pass command line switches to the phantomjs process by specifying additional args to phantom.create(), eg:

phantom.create '--load-images=no', '--local-to-remote-url-access=yes', (page) ->

or by specifying them in the options object:

phantom.create {parameters: {'load-images': 'no', 'local-to-remote-url-access': 'yes'}}, (page) ->

If you need to access the ChildProcess of the phantom process to get its PID, for instance, you can access it through the process property like this:

phantom.create(function (ph) {
  console.log('phantom process pid:', ph.process.pid);
});

##Note for Mac users

Phantom requires you to have the XCode Command Line Tools installed on your box, or else you will get some nasty errors (xcode not found or make not found). If you haven't already, simply install XCode through the App Store, then install the command line tools.

How does it work?

Don't ask. The things these eyes have seen.

No really, how does it work?

I will answer that question with a question. How do you communicate with a process that doesn't support shared memory, sockets, FIFOs, or standard input?

Well, there's one thing PhantomJS does support, and that's opening webpages. In fact, it's really good at opening web pages. So we communicate with PhantomJS by spinning up an instance of ExpressJS, opening Phantom in a subprocess, and pointing it at a special webpage that turns socket.io messages into alert() calls. Those alert() calls are picked up by Phantom and there you go!

The communication itself happens via James Halliday's fantastic dnode library, which fortunately works well enough when combined with browserify to run straight out of PhantomJS's pidgin Javascript environment.

If you'd like to hack on phantom, please do! You can run the tests with cake test or npm test, and rebuild the coffeescript/browserified code with cake build. You might need to npm install -g coffee-script for cake to work.

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 12

Native requirements
12 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
dnode
Environment
{
  "local_name": "dnode",
  "alias": false,
  "peer_metadata": null
}
~1.2.0runtime
shoe
Environment
{
  "local_name": "shoe",
  "alias": false,
  "peer_metadata": null
}
~0.0.10runtime
win-spawn
Environment
{
  "local_name": "win-spawn",
  "alias": false,
  "peer_metadata": null
}
~2.0.0runtime
traverse
Environment
{
  "local_name": "traverse",
  "alias": false,
  "peer_metadata": null
}
~0.6.3runtime
browserify
Environment
{
  "local_name": "browserify",
  "alias": false,
  "peer_metadata": null
}
1.10.9development
coffee-script
Environment
{
  "local_name": "coffee-script",
  "alias": false,
  "peer_metadata": null
}
~1.7.1development
express
Environment
{
  "local_name": "express",
  "alias": false,
  "peer_metadata": null
}
3.2.5development
temp
Environment
{
  "local_name": "temp",
  "alias": false,
  "peer_metadata": null
}
~0.4.0development
ps-tree
Environment
{
  "local_name": "ps-tree",
  "alias": false,
  "peer_metadata": null
}
~0.0.2development
vows
Environment
{
  "local_name": "vows",
  "alias": false,
  "peer_metadata": null
}
~0.7.0development
bluebird
Environment
{
  "local_name": "bluebird",
  "alias": false,
  "peer_metadata": null
}
~1.2.3development
phantomjs
Environment
{
  "local_name": "phantomjs",
  "alias": false,
  "peer_metadata": null
}
~1.9.7-5development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "shasum": "4fd2b15e931456c2b1f3b71ab1650dd4d80aceda",
  "tarball": "https://registry.npmjs.org/352-phantom/-/352-phantom-0.0.1.tgz",
  "integrity": "sha512-4F5HPcjsIe1Z1B+2JPjGfCvBGsq5JN7IlZqNA+o4vkxs691dx9z7UnGzzrJJJwGlnkt43xnK56CNuEssvEw3eA==",
  "signatures": [
    {
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA",
      "sig": "MEUCIAJ4Y4TLbeWFx7mClr8OTl0h3yB1G/N+dlQJ3K4P/XDWAiEAmsFenTE9CCApcGOH1Br/uwpdVFspocShcRIuzdYdGT8="
    }
  ]
}

Repository

PENDING
sgentle/phantomjs-node

Shared repository data · GitHub

License detected in repository
Not collected

Repository

ACCESSIBLE
dvideby0/phantomjs-node

Shared repository data · GitHub

License detected in repository
Not collected
Last push
9 Dec 2014
Archived
No
Contributor identities
— · partial collection

Public sponsor counts have not been collected. These signals belong to the repository owner.

Downloads & popularity

0count
Gauge · observed snapshots23 Oct 2026
10.50
2026-10-23: 0 · observed 2026-10-23T14:02:15.2969431Z
23 Oct23 Oct

github · 1 observations shown. Missing days are unknown.

View observed values
Date / source periodValueObserved at
2026-10-2302026-10-23T14:02:15.2969431Z

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 19:26 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:f245bead471f1ff5efcd1885fdeec35f98f9a77307421ea6e5eebd10ddf64d3b

Other collected versions

0.0.318 Jun0.0.29 DecView all collected versions →