Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

395
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda