0

Basically I have two nodeJS files which depend on each other (need values from one another) I have file A.js which need data from file B.js and exports two values:

console.log(require("./b.js"))

module.exports = {
    fromA: ()=>{console.log("message from exported A")},
    valueA: ["nesto", "josnesto"]
}

and I have a file B.js which needs module from file A.js and exports one value:

console.log(require("./a.js").fromA, require("./a.js").valueA)

module.exports = {
    fromB: ()=>{console.log("message from exported B fucntion")}
}

I've tried many options, such as trying to use promises or using a middleware file which looks like this:

module.exports = {
    fromA: ()=>{return require("./a.js")},
    fromB: ()=>{return require("./b.js")}
}

which, at the end of the day, doesn't get rid of the problem of circular dependency because linking goes this sequence:
A.js -> middleware.js -> B.js -> middleware.js -> A.js

How can i solve this?

Edit: common question was how do these files depend on each other. Well the answer is that in my original code i have splitted the program into two logical halfs which process independently yet they need some data from one another. For example, file A.js need to be able too all function from B.js when specific event is triggered in A.js, also B.js needs to have access to array (array of elements) value from A.js to be able to relay that array to the client.

The application is nodeJS application with socket.io and web-push functionalities and those files just handle users. So basicaly array from A.js is users array and i need those users in B.js so i can send their usernames to endpoints in B.js (program itself is for two parties, think about it like chat for managers and chat for employees in a business, managers have to see which employees are logged in)

  • 2
    Can you show *how* you use both imports? Currently the examples don't require each other at all. The easy solution is to make the code that depends on one library part of another library C. – paul23 Jul 18 '22 at 00:22
  • 1
    I see no actual dependency other than the fact that you put a require statement for another module (and discarded the result). Your example is oversimplified. – Wyck Jul 18 '22 at 00:35
  • a) Assign to properties on the exports object, instead of overwriting the `module.exports`. b) Do not log (or use in any other way) the imported module object *immediately*. Do it only from within the functions that you export. – Bergi Jul 18 '22 at 02:12
  • @Wyck well the original code is 354 lines long put together so i had to simplify it significantly, also its the same principle except ill be using the data in the original code and this way i just console.log it – Ivan Makevic Jul 18 '22 at 19:47
  • @Bergi thank you for your response! Could you maybe add some example code please? – Ivan Makevic Jul 18 '22 at 19:49
  • 1
    @IvanMakevic your example is exceedingly oversimplified to the point that any relevant dependency has been removed. Find some middle ground example. – Wyck Jul 18 '22 at 19:52
  • @IvanMakevic "*its the same principle except ill be using the data in the original code and this way i just console.log it*" - which data are you talking about? Do you mean the methods? And surely you don't use the in the top-level scope of the module, but rather inside some of the exported functions? – Bergi Jul 18 '22 at 19:59
  • @Bergi thank you for responding and sorry for the confusion, i've updated the question so it's hopefully more clear what my need are, best regard – Ivan Makevic Jul 18 '22 at 20:15
  • https://stackoverflow.com/questions/43023400/circular-dependencies-between-classes-why-they-are-bad-and-how-to-get-rid-of-th – Quentin Jul 18 '22 at 20:15
  • @Wyck thank you for responding and sorry for the confusion, i've updated the question so it's hopefully more clear what my need are, best regard – Ivan Makevic Jul 18 '22 at 20:16
  • @IvanMakevic Please post the actual structure of the code, with the events, the listeners, the array, the cross-module function calls, and the accessing of the array. – Bergi Jul 18 '22 at 20:20
  • @IvanMakevic Also you should consider sending a reference to the array with the respective event, not trying to access it directly. – Bergi Jul 18 '22 at 20:20

0 Answers0