There are two modules (using es6 syntax)

Module 1

 exports.someFunc = function () { return someVar; } 

Module 2

 let Module1 = require("Module1.js"); let someVar = 10; console.log( Module1.someFunc() ); /* Выбрасывает ошибку, что someVar не определена*/ 

Is it possible to somehow make the function of the first module see the local variables of the second module ?

PS Passing a variable as a parameter of the function of the first module is not very suitable
In reality, the first module is a list of functions that are called in the second module .
(There are a lot of functions, so I decided to put them in a separate module).

  • one
    Even if you can do so, it is an anti-pattern. en.wikipedia.org/wiki/Circular_dependency - Oceinic
  • I do not understand what prevents all the same to pass the variable as a parameter? She and so will fall into the memory. In addition, this is the most obvious and simple solution. - Razzwan
  • @Razzwan, the number of parameters will increase, and I don’t want to see in the code that the thread is ala func(var1, var2, var3... varN) I will try the Olson-a option - ThisMan
  • one
    Yes, and it turns out the DI container. The best solution at the moment. - Razzwan

1 answer 1

No, it is impossible, and it is good that it is, because it violates the principle of encapsulation .

But still you can use the object that both modules see:

module1.js

 let data = {}; exports.data = data; exports.someFunc = function () { return data.someVar; }; 

module2.js

 let module1 = require("./module1.js"); module1.data.someVar = 10; console.log(module1.someFunc());