Check out a free preview of the full Intermediate Gatsby, v2 course

The "Monorepo & Deployment Setup" Lesson is part of the full, Intermediate Gatsby, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Jason walks through setting up the course monorepo package.json, installing dependencies, linking the monorepo to Netlify, and starting a development environment on local host. A student's question regarding if Gatsby will recursively detect which dependencies need to be installed, is also covered in this segment.


Transcript from the "Monorepo & Deployment Setup" Lesson

>> Let's get our mono repo set up, so the first thing I'm gonna do is out in the main repo. And so this is something that's a little bit different from a non mono repo. If you're building in a standard way, you would put all of this the source folder, the Gatsby config, the package JSON, right on the root.

But because we're gonna be working on multiple packages one's gonna be the site, one's gonna be our theme, we need a way to differentiate those in our repo. So I created a folder for the site, and now we need to tell yarn that it exists, right? So we're gonna put another package JSON in the root and inside of it, we're gonna tell it, just the bare minimum that it needs to know.

So we wanna make sure that this doesn't get published to NPM, that is not what this is for. Then we're going to say that we're gonna use workspaces, and the way that workspaces work is we give them an array of folder names that are the workspaces we wanna use.

So I'm going to set site, which matches this here site, and that will tell yarn that we wanna use that particular site. Then, because I want some shortcuts, I'm gonna add some scripts. So my develop is going to run the site development command, and the reason I'm adding a shortcut, is it's a little verbose.

So if I run yarn workspace site develop, that's what will cause this site to run what you would run with NPM run develop or yarn develop. Because I don't wanna have to type that every time now I can do yarn develop, and it will run yarn workspace site develop.

Same thing with the build command, I just want to alias that, so yarn workspace site build. And that gives us our ability to shorthand NPM, run, develop NPM run build. And that's it for this so what we can do now is we can go over to our terminal, and I can install my dependencies.

So we start out by installing all of our dependencies, and what's interesting here is I ran this in the root. But because it saw this workspace, it went into this site, and found its dependencies as well. And so we can see here that all the stuff we need for Gatsby, is actually up here at the top level, instead of being in the site folder.

And that's really convenient because that also means when you're working with workspaces, if your packages share the same version of something, you don't have to install it twice. You'll just have one node modules folder, and then it kinda does this alias here so that we've got access to the Gatsby command inside of the site.

And then, now that we've got this set up, we can, We're gonna tell Netlify how to find our mono repo. So we're gonna create a new file called netlify..toml, and in this, we're just gonna tell this is for the CLI to know where the site is. So we're gonna set base site, and that's just to say when you build this, the site is what we're gonna publish.

The theme is a supporting file that will actually be installed by the site, so when we publish this mono repo the only thing that we want to build, is the site so we can move netlify into that folder. And finally, now that we've installed our dependencies, we can run netlify dev, and netlify dev is a CLI command that gives us a lot of power.

It auto detects what you're using, so it can see what framework we're using, we're using Gatsby. It runs the develop command for us, and you can see here that it's kinda doing some environment variable stuff, that'll become more useful later. So then it runs our Gatsby develop command, And once this is done here, it's actually gonna pop open a browser for us.

>> It has mauled out so, when we were setting up our yarn file write so what it was was it detects the package.JSON inside the site for our folder, right? So when we set up a couple of more folders like you said a theme, and maybe we are having a couple of themes.

So will the yarn file detect each and every package JSON or recursively, through those file structure and install all of the dependencies?
>> No so, what it will do is it will detect the ones that we've told it to so, we told it to look at site. And so it's gonna look in here and actually this is a good point, it uses the name in here, which can be kind of confusing.

So, when we did yarn workspace site develop, it's looking for this package name, and not the name of the folder. So that can be a little bit confusing, so if this folder was called websites but the package in here was called site, you would put website as the workspace.

But you would still run yarn workspace site develop, so that's why I always named the folders the same thing, cuz otherwise I get really confused and it has a hard time. If you want it to auto detect a whole group of things, you can do I think it's either a star or double star, we're not gonna use that today, but I have used it in the past.

It's documented well on the yarn workspaces docs if you wanna do that, but that will auto detect the packages inside of that folder. So that can be really nice if you're working on a mono repo that's got all of your your open source node packages or something like that, where you don't wanna have to explicitly declare each one.

Okay, so we're up and running, we've got the site is functioning now a slight difference Gatsby goes to 8,000 netlify dev goes to 8888. That's because it is running Gatsby at Port 1000 and then proxying it along with the serverless functions. So that way we were able to go in, and otherwise everything is exactly the same.

If we come in here and make a change, then it's auto updated everything happens very, very quickly. So great, we have a development environment, were up and running now.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now