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

How to discover and load js files at runtime?

How to discover and load js files at runtime?


I'm writing a node.js based server that manages a range of devices. The node.js based server tells connected clients about its abilities. The abilities are defined by separate js files that define objects that are using inheritance via util.inherits().

The problem I have is that right now, I have to define a new js for a new ability and then update the main js program to require the new js, change the code to publish that the ability is available, and then utilise the new ability if requested to by the client.

I would like to make the main code more generic whereby it can

  1. detect the new abilities,
  2. automatically include them,
  3. notify the clients, and
  4. utilise the code.

The detection I can do via the various npm modules out there that support tree browsing, I can just nominate a subdirectory for all capabilities and discover what files are there. I presume that I can use require for step 2 (though not 100% certain), however I don't know how to do step 3 and 4 or use the results from step 2 with step 3 and 4.

I would value any feedback on how to solve this problem.

To clarify my problem. Right now my Logic is as per the following:

var logicA = requires('./capabilities/a.js');
var logicB = requires('./capabilities/b.js');
var logicC = requires('./capabilities/c.js');

var Comms.CAPABILITY_A = 'a';
var Comms.CAPABILITY_B = 'b';
var Comms.CAPABILITY_C = 'c';

var Comms.MSG_CAPABILITY = 0;
var Comms.MSG_DO_LOGIC = 1;

function onMessageReceived(comms, msgId, body) {
    switch (msgId) {

function doLogic(flag) {
   switch(flag) {



At the client side I have hard coded logic that presumes what is available. I can remove this by having the server send an array of the capabilities to the client, and then the client can choose one of the elements of the array and pass it back as the request to execute the logic. This is not my problem.

My problem is understanding how to cause the host program load all the logic dynamically and then evaluate which logic to execute on the dynamically loaded logic.

I should state that when I say dynamic, I mean that the code available is determined at runtime. However the evaluation is only ever performed when the server is first started.

Problem courtesy of: Metalskin


I solved the problem by creating a register.js where all the protocols are kept. Each time I create a new Protocol, I add it to the register.

Via the register I can get an array of all Registered protocols. I can pass them back to the client, the client can choose a protocol and I can request an instance of the protocol via the register class.

While there is some hardcoding, it's restricted to the register class which is in the same directory as the protocols.

So in the register I have the following functions:


I use getList() to return an array of the protocol id's that are registered. I use getText() to provide a human readable list of supported protocols. I use validateProtocolId() to validate an id returned from the client to confirm that the id represents a registered protocol and then I use getProtocol() to generate an instance of the registered protocol.

In essence the getProtocol() just does a require('./.js') as appropriate.

It's not as elegant as auto discovery, but it allows tighter controls on what is registered without forcing custom file, etc.

Solution courtesy of: Metalskin


View additional discussion.

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

Share the post

How to discover and load js files at runtime?


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription