Locate circular dependencies in TypeScript modules

Published: Jun 11, 2018

Lately, I was researching how to compile [email protected] project using angular-compiler - ngc. And found out that one of a reason an Angular project could fail to compile are circular dependencies in TypeScript files. After a quick research, I found a great developer tool madge which allows finding such dependencies.

Madge is a developer tool for generating a visual graph of your module dependencies, finding circular dependencies, and give you other useful info.

Let’s take a look on a trivial example where we have two TypeScript modules depending on each other.

ModuleA depends on ModuleB.

ModuleA.ts

And ModuleB with dependency on ModuleA.

ModuleB.ts

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.

A circular dependency found: moduleA depends on moduleB

That's it! Now you see where you have circular dependencies and can go and fix it.

I hope you will find this little hint useful and it will help you to improve the quality of your TS projects. I encourage you to explore the tool as it provides much more than just circular dependency finding. And see how you could incorporate it in your web development process.