Generate a Dependency Graph for Monorepos Using Turborepo

Share this video with your friends

Send Tweet
Published 5 months ago
Updated 4 months ago

A dependency graph is exactly what it sounds like! A visual graph created by Turborepo to show you your dependencies and what dependencies the projects rely on. For our Monorepo, we'll run npx turbo run build --graph in the root of our project and that will build the dependency graph for all of our projects.

We can also do one with a graphic but that requires the Graphviz library. We can edit our command to have some arguments: npx turbo run build --graph=graph.pdf and it will create a graphic in a .pdf file.

Instructor: [0:00] When you have a monorepo, it is difficult to keep track of what depends on what in terms of packages inside the monorepo. Turborepo has this super handy tool that will generate a graph for you so you can understand better what is going on. [0:18] To see the dependencies in the build pipeline, we have to add the --graph flag at the end. This is going to tell us what depends on what. In this case, the build command in the blog project depends on the @monorepo/utils#build, the dashboard build depends on the @monorepo/utils#build, and the @monorepo/utils#build depends on nothing.

[0:43] We can also generate graphics. For that, we will need to install the Graphviz library first. You can see how to install it in your system depending whether you have Linux, Windows, or macOS.

[0:58] I already have Graphviz installed in my system. All I need to do here is to give graph some arguments. I'm going to use the equal sign here. I'm going to say graph.pdf. As you can see here, it generated a PDF inside the Turborepo project.

[1:22] If we open that PDF, it is telling us basically the same that we saw in the output in the terminal. These two, dashboard and blog, depend on utils and utils doesn't depend on anything else. You can even generate the graph as an HTML page just by changing the extension from .pdf to .html and it will generate this HTML page for you.

[1:48] To recap, Turborepo has this handy --graph flag that we can use to generate files that explain how our dependencies are connected together. We can generate different file types. For instance, what we have to do to generate a different file is to just specify the name and then add the extension that we want.

[2:11] It could be a JPEG. It could be an HTML page. It could be a PDF file as well. If we don't want to generate any file, we can just use the --graph flag as it is. It will generate this output into terminal.