Finding circular deps in Node.js projects
Circular dependencies in Node.js modules can be tricky to find if you create them accidentally. I wrote a simple bash script that lists intermodule dependencies and prints them in a way that tsort(1) understands.
Combining these two programs can list circular dependencies in a project:
$ find-requires -e node_modules ~/my-project | \ tsort >/dev/null tsort: cycle in data tsort: lib/a.js tsort: lib/b.js tsort: lib/c.js
(I discard the normal output of
tsort since cycles are printed to
There are two things that make this solution slightly interesting:
- You can often get reasonable results quickly using a naive solution. This
dependency "parser" simply greps sources for
require()calls, which isn't perfect, but is simple and was very quick to implement.
tsortis a good example of the Unix Philosophy. It's easy to plumb such programs together, using them for things their creators hadn't envisaged. I wish more utility authors embraced this philosophy :-)