Locate circular dependencies in TypeScript modules

Image for post
Image for post

Lately, I was researching how to compile Angular@6 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.

Image for post
Image for post
ModuleA.ts

And ModuleB with dependency on ModuleA.

Image for post
Image for post
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.

If you found this post useful and would like to read more about random web development topics, just clap for this article or drop a comment here. And as always you can find me on Twitter@andrejsabrickis

And if you are looking for new career opportunities, take a look at our open positions at KASKO.io. Join us and let’s reshape the multi-trillion dollar insurance world together.

Cheers!

Writing JS, TS, Vue, #C, and fostering teams to release customer value n-times a day. Creator of billid.app and writer on abrickis.me

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store