node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »



Define and enforce module interfaces


npm install interfacejs


We use this by defining an interface in its own file: interfaces/

module.exports = {
  load: true,
  save: true,
  exists: true

Then some file that wants to do stuff with a cdn can require a dependency that meets this interface:

module.exports = interfacejs(require('../interfaces/cdn'), function (cdn) {
  return {
    getFile: function (name, cb) {
      cdn.load(name, cb)

in coffeescript:

module.exports = interfacejs require('../interfaces/cdn'), (cdn) ->
  getFile: (name, cb) ->
    cdn.load name, cb

Now there can be many different implementations of cdn, something like:

// this could be made to hit any cdn
module.exports = function (cdnUser, cdnPass) {
  return {
    load: function () {
      // do stuff for loading a file
    save: function () {
      // do stuff for saving a file
    exists: function () {
      // do stuff to check if file exists

When someone wants to use our module that uses a cdn, just pass in our preferred cdn:

cdn = require("./cdns/s3")("me", "gusta")
module = require("./module")(cdn)

Running Tests

npm install -g mocha mocha test.js