Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

393
Views
How to solve circular dependency in nodeJS?

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)

about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!