Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Circular Dependencies in ES6

Tags:

Is there a way to detect (static analysis tools like eslint, ??) Circular Dependencies in javascript. More like

module A imports module B module B imports module A 

I had some trouble with this when using @flowtype and like to see where it happening in my moderate size codebase.

My code use ES6 module system and uses babel + webpack to bundle it.

like image 216
bsr Avatar asked May 26 '16 13:05

bsr


People also ask

How do I find circular dependencies?

By running a cli command npx madge --circular --extensions ts ./ we can quickly get a list of circular dependencies of all . ts files in current directory and its subdirectories.

Are circular dependencies ever OK?

and, yes, cyclic dependencies are bad: They cause programs to include unnecessary functionality because things are dragged in which aren't needed. They make it a lot harder to test software. They make it a lot harder to reason about software.

How do you prevent circular dependencies?

To reduce or eliminate circular dependencies, architects must implement loose component coupling and isolate failures. One approach is to use abstraction to break the dependency chain. To do this, you introduce an abstracted service interface that delivers underlying functionality without direct component coupling.

Does Webpack resolve circular dependencies?

It is caused by circular dependencies that cannot be resolved synchronously by webpack. One is undefined, hence the error.


1 Answers

You can use the circular dependency plugin for webpack: https://www.npmjs.com/package/circular-dependency-plugin

You can also use madge to statically detect circular references: https://github.com/pahen/madge

like image 191
wlingke Avatar answered Sep 22 '22 23:09

wlingke