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)