title: How to solve circular dependency in nodeJS?
body: <p>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:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>console.log(require("./b.js"))
  
  module.exports = {
      fromA: ()=&gt;{console.log("message from exported A")},
      valueA: ["nesto", "josnesto"]
  }</code></pre>
  </div>
  </div>
  </p>
  <p>and I have a file B.js which needs module from file A.js and exports one value:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>console.log(require("./a.js").fromA, require("./a.js").valueA)
  
  module.exports = {
      fromB: ()=&gt;{console.log("message from exported B fucntion")}
  }</code></pre>
  </div>
  </div>
  </p>
  <p>I've tried many options, such as trying to use promises or using a middleware file which looks like this:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>module.exports = {
      fromA: ()=&gt;{return require("./a.js")},
      fromB: ()=&gt;{return require("./b.js")}
  }</code></pre>
  </div>
  </div>
  </p>
  <p>which, at the end of the day, doesn't get rid of the problem of circular dependency because linking goes this sequence:<br />
  A.js -&gt; middleware.js -&gt; B.js -&gt; middleware.js -&gt; A.js</p>
  <p>How can i solve this?</p>
  <p>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.</p>
  <p>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)</p>