Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to structure classes within a node module?

How to structure classes within a node module?


When working with lots of Classes in a node Module is the following mechanism a good idea or is there a better way to achieve this?

// mymodule/core.js

module.exports = {
    ClassA: require('./class/ClassA'),
    ClassB: require('./class/ClassB'),
    ClassC: require('./class/ClassC')

// mymodule/class/ClassA.js

module.exports = function() {

// myapp.js

var core = require('mymodule/core')

var a = new core.ClassA();

The idea of the above is to keep classes in physically separate modules for maintenance whilst providing a namespace that is easy to use.

Problem courtesy of: Lea Hayes


For me it is far clearer to have the modules you are using pulled in within the module using them. You can't get around the fact that the module has these dependencies and it will be easier for someone else reading the code to see what is being used.

Also if you ever wanted to reuse a single module you would have to also carry with it the core.js module–and all of it's dependencies–from which your module is coupled.

Solution courtesy of: Phil Parsons


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How to structure classes within a node module?


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription