6

A node.js module of mine got too big, so I split it into several smaller (sub)modules.

I copy & pasted all relevant objects into each of the submodules, which now look like

var SOME_CONSTANT = 10;

function my_func() { etc... };

Now I want to export everything in each submodule, en masse, without having to explicitly say exports.SOME_CONSTANT = SOME_CONSTANT a million times (I find that both ugly and error prone).

What is the best way to achieve this?

2 Answers 2

1

I assume you do not want to export every local variable.

I will get around to automating this one of these days, but for now I often use this technique.

 var x1 = { shouldExport: true  } ; 

// create a macro in your favorite editor to search and replace so that

x1.name = value ; // instead of var name  = value

and

name becomes x1.name   

// main body of module

for ( var i in x1) { exports.better_longer_name[i]   = x1[i] ;} 
//or if you want to add all directly to the export scope  
for ( var i in x1) {  exports[i] = x1[i] ; }  
Sign up to request clarification or add additional context in comments.

Comments

0
module.exports = {
    SOME_CONSTANT_0 : SOME_CONSTANT_1 ,
    SOME_CONSTANT_1 : SOME_CONSTANT_2 ,
    SOME_CONSTANT_2 : SOME_CONSTANT_3
}

so why you need that "million" constant to exports?

2 Comments

Because this duplicates code (DRY, inviting errors), plus is ugly. I'd prefer an automated solution that I could copy&paste into each submodule.
I think you need some trick to rebuild your code, e.g. replace = to : and ; to ,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.