Set of scripts that make PhantomJS's environment more similar to Node.js.
JavaScript
Switch branches/tags
Nothing to show
Pull request Compare This branch is 2 commits ahead, 17 commits behind jgonera:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
modules
LICENSE
README.md
coffee-script.js
nodify.js

README.md

phantomjs-nodify

Set of scripts that make PhantomJS environment more similar to Node.js. I implemented what I needed for my scripts. Feel free to add more.

Partially implemented features:

  • Experimental require() support for custom (not built into PhatnomJS) modules. Use relative or absolute paths to include your modules (i.e. they must start with ., .. or /).
  • Exceptions thrown from required files are properly reported (with file name and line number). Line number for .coffee files may not be accurate.
  • Global process object (some basic functionality + emits uncaughtException on exceptions that occur inside setTimeout blocks).
  • console with string formatting (e.g. console.log('hello %s', 'world')).
  • Some Node.js modules (see modules dir).
  • Other minor tweaks.

Some code taken from Node.js, CoffeeScript and mocha.

How to use

Clone:

git clone git://github.com/jgonera/phantomjs-nodify.git

Inject in your PhantomJS script at the very first line:

    var nodify = 'phantomjs-nodify/nodify.js';
    phantom.injectJs(nodify);

You must provide the path to nodify.js in the global nodify variable.

Then, wrap your script in nodify.run():

    var nodify = 'phantomjs-nodify/nodify.js';
    phantom.injectJs(nodify);

    nodify.run(function() {
      var assert = require('assert');
      var myModule = require('./mymodule');
      // your script here
    });