Initial Turborepo setup
This lesson preview is part of the Bundling and Automation in Monorepos course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to Bundling and Automation in Monorepos, plus 90+ \newline books, guides and courses with the \newline Pro subscription.

[00:00 - 00:08] In this module we're going to add Turborepo to our monorepo. Why use Turborepo instead of plain pnpm scripts?
[00:09 - 00:25] Well, we have already used pnpm a bunch and it works fine for simple stuff. But as your complexity grows, you do want to reach for something better suited for handling that complexity.
[00:26 - 00:56] If we go part by part, what I've written down here is that while pnpm can run tasks in dependency order, so if you have an internal UI package that has a build script and you have an application that has a build script, pnpm will correctly execute the internal package build first and then the application build. Turborepo allows you to actually specify much finer grained dependencies. You can say that some tasks depend on multiple other tasks.
[00:57 - 01:24] For example, for some projects it might be necessary to do a build before you can run tests. If you tests run, let's say build outputs that are binaries and Turborepo can capture these sort of more complicated inter-task dependencies, while pnpm will only run tasks with the same name in dependencies. Then the next one is that pnpm always executes each script every time.
[01:25 - 01:44] If you have some internal incremental logic to the script itself, then it might be faster on a second run. While Turborepo allows you to define inputs and outputs to a task, and when the inputs are the same, it doesn't run it at all, it just gives you the cached output.
[01:45 - 02:01] Additionally, this works remotely. You can cache outputs from a command that has run either on another server or on another developer's machine, and have it be executed for your local machine.
[02:02 - 02:15] This means that if someone is performing the same type of work as you are, or if there was a CI server that performed the same work as you are, you're going to get that benefit and not run it yourself. Instead, you're going to immediately get the output.
[02:16 - 02:35] pnpm doesn't track what files are produced from a script. While Turborepo allows you to declare outputs and it will cache those and restore them automatically from cache, even if you delete them yourself. When you run the task, it's going to be recreated.
[02:36 - 02:59] pnpm has no concept of graph visualizations, while Turbo has a very neat way of showing you the dependencies for a particular task, you can directly export an image that will show you what your graph of dependencies looks like. And finally, for extra utilities, the only thing that pnpm can do is filtering.
[03:00 - 03:29] While Turborepo has several quite useful utilities, "turbo prune", for example, is going to create a limited version of your monorepo that only has a couple of packages that you care about. "turbo graph" allows you to visualize task graphs, and "turbo ls" gives you overview of the available tasks for your monorepo or packages. To put it another way, we are going to be using Turborepo because it makes it faster to rerun scripts.
[03:30 - 03:52] It allows for finer control of your dependency graph. It allows for better visibility of how your dependencies are connected and it scales across teams and CI. So with all of that in mind, I'm going to go to my monorepo and I'm going to "pnpm add -D -w turbo".
[03:53 - 04:07] Which is going to add Turbo to our workspace route. I'm then going to run "pnpm turbo scan", which is going to help us ensure we have Turborepo set up correctly.
[04:08 - 04:14] So we see two failures here. The first one is that the Turborepo extension is not running.
[04:15 - 04:27] This is because I'm currently running in a normal terminal. When it says the extension is not running, it's expecting that this would be a terminal within VSCode and there would be a VSCode extension.
[04:28 - 05:00] If I go back to VSCode, I can go to my extensions tab, and make sure that I have the Turborepo LSP extension, which is going to just make things a bit easier if you're using VSCode for you. And I'm also going to open a terminal within the terminal I can run "pnpm turbo scan", and now we have okay, we have the extension running and we have the demon for Turbo running.
[05:01 - 05:15] The demon is just an additional process that Turbo is using to speed up task execution. And finally, the last complaint is that we don't have a user attached to our Turborepo.
[05:16 - 05:36] I'm going to show the setup for this just because I suggest running with a login for Vercel. It's going to enable remote caching. So let's do that. I'm going to do "pnpm turbo login" which is going to pull up the Turborepo authorization window.
[05:37 - 05:55] If you don't have an account with Vercel, you can register one with your Google account. I'm going to authorize the CLI, and when we go back, we can see that Turborepo CLI has now been authorized. I'm going to clear the terminal because I don't want to show my email.
[05:56 - 06:07] And I'm going to then do "pnpm turbo link". This will enable remote caching for our monorepo locally, and I do want to do that.
[06:08 - 06:17] It's going to use my profile, VanTanev's projects. And now if we just run "pnpm turbo scan" again we see all green.
[06:18 - 06:28] So this is where you want to end up in the end, where you're all green on your "turbo scan". This indicates that you have completed your Turborepo setup.
[06:29 - 06:59] I'm going to go back to my normal terminal, clear this and do "git status" just to see what we have here. We need to add one thing to our .gitignore, which is we need to ignore folders with ".turbo". This is where Turbo is going to locally store the cached outputs of commands or of file changes that your scripts create.
[07:00 - 07:23] And with that in place, you can just add everything and commit it as "Initial Turborepo setup". Now, because we currently don't use the Turbo command for anything, this fails. We get a Knip error that we have a dependency on Turbo, but we're not using it for anything.
[07:24 - 07:40] In the next lesson, we're actually going to start using Turbo. So for now, I'm going to allow this failure and commit with it using "--no-verify" to skip running our normal pre-commit check.
[07:41 - 07:44] And that's everything for this lesson. I will see you in the next one.