Skip to content
Wade Womersley

wade.one

wade womersley – york based software engineer

  • Home
  • 2013
  • December
  • 9
  • phantomjs custom module – require and create

phantomjs custom module – require and create

Posted on December 9, 2013 By Wade No Comments on phantomjs custom module – require and create
Programming

Recently I’ve been working with phantomjs in order to do some on-page control without wanting to use an actual browser (phantomjs is headless and requires no X server to be running).

One of the first things I wanted to do was created custom modules so I could organise my code clearly. I found a surprising lack of information on this topic so decided to quickly write up how I did it so it follows how the included modules work.

To use a custom module, you need to prefix the require with a path (./ works fine for the current directory) and exclude the .js extension:

var mymodule = require('./mymodule');

So in this case, alongside your primary JS file, you’d have a mymodule.js file.

Next, in mymodule.js, you’d write code something like this:

function MyCustomModule(){
...your code here...
}

MyCustomModule.prototype.someFunction = function(){
...your function code...
};

exports.create = function(){
    return new MyCustomModule();
};

This will allow you to return a new instance of your MyCustomModule class while using prototyping.

So going backing to the original require, to create a new instance you could do the following:

var customModule = require('./mymodule').create();

It’s really simple and obvious once you see it, there just was no where to see it!

Share:

  • Share on Facebook (Opens in new window) Facebook
  • Share on X (Opens in new window) X
  • Share on Tumblr (Opens in new window) Tumblr
  • Share on Pinterest (Opens in new window) Pinterest
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Reddit (Opens in new window) Reddit

Related

Comments

comments

Post navigation

❮ Previous Post: C# MySqlDataReader – “one line” function to return a row as an instance of a class.
Next Post: Ultra High Definition TV Coming Soon ❯

You may also like

Programming
Why Kotlin Coroutines Are Worth the Learning Curve
April 14, 2026
PHP
PHP 8.5’s Pipe Operator Is for Readable Code, Not Clever Code
May 9, 2026
PHP
PHP 8.5’s URI Extension Fixes a Real Web Problem
May 8, 2026
Programming
#leedsphp talk slides uploaded
April 22, 2011

Leave a Reply Cancel reply

You must be logged in to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • AI
  • artificial intelligence
  • Ego-centric
  • Events
  • Films
  • Food
  • Gaming
  • Gym
  • Hardware
  • Holidays
  • News
  • PHP
  • Programming
  • Random Stuff
  • Reviews
  • Science
  • SEO
  • Software
  • Software Engineer
  • Support
  • Uncategorized
  • Work

Copyright © 2026 wade.one.

Theme: Oceanly News Dark by ScriptsTown