Export all objects in node.js

A node.js, my module became too big, so I split it into several smaller (sub) modules.

I copy and paste all the 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 to each submodule in bulk, without explicitly exports.SOME_CONSTANT = SOME_CONSTANT million times (I believe that both are ugly and error prone).

What is the best way to achieve this?

+6
source share
2 answers

I assume that you do not want to export each local variable.

I will manage to automate this the other day, but for now I often use this technique.

  var x1 = { shouldExport: true } ; 

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

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

and

 name becomes x1.name 

// main module 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] ; } 
+1
source
 module.exports = { SOME_CONSTANT_0 : SOME_CONSTANT_1 , SOME_CONSTANT_1 : SOME_CONSTANT_2 , SOME_CONSTANT_2 : SOME_CONSTANT_3 } 

so why do you need to export this "millionth" constant?

0
source

Source: https://habr.com/ru/post/916201/


All Articles