1

I have bunch of functions declared across my modules, which I would like to use as global constants for all my codebase.

Now these functions declared as local constants:

const { function1, function2, function3, ..., functionN } = require('./my-module-1')
const { functionA, functionB, functionC, ..., functionN } = require('./my-module-2')

Is there shorter way to convert these function to globals instead of:

function1 = require('./my-module-1').function1
function2 = require('./my-module-1').function2
function3 = require('./my-module-1').function3
functionN = require('./my-module-1').functionN

functionA = require('./my-module-2').functionA
functionB = require('./my-module-2').functionB
functionC = require('./my-module-2').functionC
functionN = require('./my-module-2').functionN

I've tried just removing const keyword as well as added brackets around the destructuring assignment but both caused syntax error

{ function1, function2, function3, ..., functionN } = require('./my-module-1')
{ functionA, functionB, functionC, ..., functionN } = require('./my-module-2')

({ function1, function2, function3, ..., functionN } = require('./my-module-1'))
({ functionA, functionB, functionC, ..., functionN } = require('./my-module-2'))
Systems Rebooter
  • 1,281
  • 2
  • 14
  • 30

1 Answers1

0

My suggestion,

global.modules = require('sample-package'); // get all modules and set it to global object

let { module1 } = global.modules; // where you want use this

let { module2 } = global.modules; // where you want use this
Vasi
  • 1,147
  • 9
  • 16