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?
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With