AMAZON CONTENT

[Node Developer must know] What is Node Circular Dependency ?

What is Circular Dependency in Node.js ?



Team, before a day ago I was doing coding for one Lambda function. Existing lambda function was working properly since developed, but suddenly it stopped. I check my logs, it was saying 'X' is not a function. I shoked ... I though for a while, what can be the possible root cause of this certain in certainity. 

Let me go through in depth ... what I was doing. 
  • I had one file called getData.js (which was containing business logic of my endpoint)
  • Another file named, dbQueries.js (which was containing common function for db operations)
  • Another file named, utils.js (which was containing common functions)
At some level, I need one function to get some data from database, I thought lets put this inside utils.js as it is containing common functions. I created that function, deployed my code to AWS.... and then after errors came.... 'X' is not a function 😒

Do you know why it was happening ? Because of one simple mistake I made. 
  • One of my utils file function was imported into dbQueries file. 
  • One of my dbQueries function was imported into utils file. 
I guess ... you got my point .... This is circular dependency in Node.js. 

As we are not able to get any function because of this, that why it was giving error of - 'X' is not defined. Hope, your funda is now clear about this new learning, let us know if you have any doubt. 

Subscribe us 📨 and Stay tuned with us. Happy coding!!!

Post a Comment

0 Comments