Skip to content
wade.one

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:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X
  • Click to share on Tumblr (Opens in new window) Tumblr
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to 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

PHP
PHP 8.2 vs 7.4
March 26, 2023
Programming
PHP5.4.3 Available on Dotdeb.org (Installs fine on Ubuntu)
May 22, 2012
Hardware
Superb VPS (Virtual Private Server) Provider – VPS.net Review
November 18, 2009
Programming
PHPNW09 – PHP North-West 09 Conference
October 10, 2009

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