Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect circular dependencies in project

Last time i found in my project problem with circular dependencies. I resolve my problem but, i would like avoid this in future. I think about plugin which found circular dependencies in my all project and give me feedback.

Example:

File a.js:

var functionFromA= require("./b.js");
console.log("file a", functionFromA);
module.exports = {functionFromA: functionFromA};

File b.js:

var functionFromB = require("./c.js");
console.log("file b", functionFromB );
module.exports = {functionFromB : functionFromB };

File c.js:

var functionFromC = require("./a.js");
console.log("file c", functionFromC );
module.exports = {functionFromC : functionFromC }

When i run file a.js i see in console:
file c {}
file b { functionFromC: {} }
file a { functionFromB: { functionFromC: {} } }

I found "Circular Dependency Plugin" in npm but i don't know how use it? May be someone has similar problem and found a solution?

like image 362
new_user Avatar asked Jun 04 '17 16:06

new_user


People also ask

What is a circular dependency?

A circular dependency is where Project A depends on something in Project B and project B depends on something in Project A. This means to compile Project A you must first compile Project B, but you can't do that as B requires A to be compiled. This is the problem that circular dependencies cause.

Are there any node packages that check for circular dependencies?

While there are quite a few Node packages that perform static analysis to look for circular dependencies, they didn’t quite do the trick. Some of the packages found a few circular dependencies, while others missed all of them completely. The best circular dependency checker that I found works at the bundling layer.

How do you get rid of circular dependencies in Java?

Fixing the Circular Dependencies There are a couple of options to get rid of circular dependencies. For a longer chain, A -> B -> C -> D -> A, if one of the references is removed (for instance, the D -> A reference), the cyclic reference pattern is broken, as well. For simpler patterns, such as A -> B -> A, refactoring may be necessary.

Is it okay to have a circular dependency inside a library?

Moreover, it may be okay to have a small circular dependency inside a small module/library but not through the whole project. Design patterns like Dependency injection, Observer or Event Queue can also be of great help.


2 Answers

You can find this other tool useful: https://www.npmjs.com/package/madge

is very easy to use: madge --circular [directory] give you a list of circular dependencies in your code located inside [directory]. It can also generate a graph image.

like image 77
davidmpaz Avatar answered Sep 22 '22 23:09

davidmpaz


As davidmpaz suggested, madge is a good tool for such a thing but it only detects circular dependencies and doesn't indicate whether these circular dependencies causes a problem in your application or not.

I made a tool to detect the circular dependencies and it warns you about the problem that caused by cd. https://www.npmjs.com/package/detect-circular-deps

like image 44
Ahmed Sabry Avatar answered Sep 23 '22 23:09

Ahmed Sabry