How to Deploy Your First React App on Netlify

In this final lesson of the module, we'll walkthrough the deployment process to host our Furry Friends Gallery Mark II app on the Netlify hosting platform.

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Beginner's Guide to Real World React 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.

This video is available to students only
Unlock This Course

Get unlimited access to Beginner's Guide to Real World React, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Beginner's Guide to Real World React

Lesson 3. Setting up deployment on Netlify. From the previous lessons we've discovered that we'll need suitable hosting platform to serve our build and process apps, websites and SPAs. Because the code you're likely to produce with React is dependent on some kind of build process to get from development code to production ready output, it's useful to choose a hosting provider that can handle this building process for you. You could of course just run build commands manually then deal with the output yourself, but this is a tedious, unnecessary and very error-prone process. In modern development environments it's much more common to deal with some sort of predictable build and deployment pipeline where tests are run, code is built , bundled and packaged for deployment, and physically deployed somewhere for public consumption. We've briefly introduced Netlify as an option for hosting our React apps and sites, but we'll take a look in my depth here. We're going to sign up for a new account, attach Netlify to our GitHub repositories, set up a build and deployment project for the furry friends gallery Mark 2, and finally deploy our app to the world. But why Netlify? In the previous lesson we looked at a range of different options available for hosting while complex projects such as SPAs and React driven apps and websites. The reason for picking Netlify is that it has the perfect blend of simplicity, flexibility and power. It's simple and straightforward enough for beginners and those new to deploying their apps to grasp, but powerful enough to run huge enterprise level applications. You'll see the likes of Nike, Victoria Beckham and Live chat as but a few of their customers. Whilst hosting options such as AWS can introduce a steeper learning curve as they have many distributed and interconnected services that need to be wired together, they also bring with them a complex pricing model that means you can be potentially paying for services that you have mistakenly left running in the background. Netlify offers a much more straightforward approach focusing on lightning fast CDN backed hosting with some really cool add-ons and plug-ins. Netlify's benefits include a simple three-step process from connecting your code repository to reviewing a deployed app, the use of custom demands, free SSL certificates as standard, build time plug-ins such as image optimization as your code project is building, form capturing add-ons to deal with any form submission on your website, preview builds of your site before committing the full deployment and automatic repository watching. Your site builds and deploys every time you make a commit to a specific branch. All of this can be managed through a clean, user-friendly GUI but power users also have the option of using a command-line interface if they prefer. For our purposes we'll start with creating a new Netlify account and wiring up our furry friends gallery mark 2. Step 1 - Create a new account on Netlify. This is the easy part. Head over to www.netlify.com and you'll be greeted with the company's landing page. Look for the sign up button in the top right hand side of the screen and click it. You'll be taken to the sign up screen which will look like this. Feel free to sign up however you wish. You'll see that Netlify offers a range of options including a simple email password combination. For the purposes of this lesson we'll be logged in with GitHub as it is arguably the most common. However, even if you choose another provider, the meat and potatoes of the lesson here will work in the same way. The main difference will be around signing, choosing a repository in any special GitHub commands. Step 2 - Create a new project for the furry friends gallery. If you choose to sign up using GitHub on one of the other code versioning platforms, you'll be whisked off to their specific sign-in screens to authenticate and then to authorize Netlify to be able to read your repository information. Once authorized you'll be taken to the Netlify dashboard. Here's mine. On the dashboard you have easy access to high level account settings such as domains, builds, billing and any team members that you have. You'll see on my dashboard a list of Netlify hosted projects. In this case my personal website, robkendall.co.uk and my podcast The Frontend, The Frontendpodcast.site. If I were to click on one of these, I'd be taken to a sub dashboard with specific information related to this site such as deploys, previews and settings. We'll look at that screen in more depth shortly but for now we're going to add a new project. Hit the green 'New site' from git button at the top right of the dashboard and you'll be taken through the new site wizard. The first step in the wizard is to choose the source for the website or app. Notice that you can sign into a Netlify account using whichever method you prefer but you're not restricted on where you pull your projects from. So you can sign up for a Netlify account with gitlab for example but then use projects from github or bitbucket if you prefer. In our case we'll choose github. So click on the github button and you might see a pop-up asking to authenticate with your github account. Once you've done that, you'll be returned to the new site wizard and we'll move to the next step, picking a repository. In this step of the wizard we need to choose the repository from where our deployable code is located. You can see mine is called real world react module 4 demo in the screenshot but yours might be called something different. An important note, you may have been developing along with this course inside of folders on your machine that aren't tied to any code versioning system like git. That's great, however if you want to deploy this code somewhere like Netlify, you'll need to have your code committed into a git repository such as github, gitlab or bitbucket. If you haven't already done that, make sure you have before continuing. So now we're going to search through our repositories and find the one where our furry friend gallery is located. Click on it to choose it and you'll be moved onto the final part of the wizard, build options and deploy. So fast or good, and quite simple. There's a little bit more to this final step but let's check out the screen to see what's going on here. You can see in this first section we've got the chance to assign an owner and a deployment branch. The owner information is only relevant if you have multiple members in your team or multiple teams. The branch to deploy however might differ depending on your git versioning approach. It's quite common to have a main or master branch in your project and this will be the primary source of truth for the project where all releases are made from . Leave this section as main or master or whatever the default branch is unless you have a specific need to deploy your code from a different branch. In the basic build settings section we'll define our build command and directory in which Netlify will look to find all the files it needs to deploy. Netlify does a pretty good job at working out what these values should be based on the type of project you're deploying. However for clarity we need to know what command to give Netlify to trigger the build process as well as the final folder that is output as part of that build process. In our case we can open up the project furry friends gallery mark 2 and look in the package Json file under the script section. Since this is a create react app you'll see the command build. If we were to build this project ourselves manually in a terminal window we'd type yarn build so that's what we have to pass the Netlify in the build command input box. Similarly if we run the yarn build command and let it finish we'd see a new folder in our project called build. This is the location of the build and processed project files ready to be deployed so we'll pass build as the folder to the published directory input here. Advanced build options. Usually this is where we'd leave things and just click the deploy site button. However we need an extra step because our furry friends gallery is using environment variables. At the bottom of this page look at the advanced build settings section where it talks about environment variables. In the previous module we introduced the concept of environment variables and how they're stored in .n files. The drawback to .n files is that they usually start on each developers local machine and not checked into code versioning systems and are therefore not available to build systems such as Netlify. When we built the furry friends gallery mark 2 in the very last module we used two environment variables to access our dog API. Namely react app dog API URL and react app dog API key. So how do we make those variables available at build time for Netlify? Fortunately like all good build pipelines Netlify provides us with the option to add environment variables to be used at build time. So if we take a look towards the bottom of the build settings screen you'll see an advanced build settings section. This is where we can add our environment variables to enable the site to connect with our dog API. Click the new variable button twice and you'll see two sets of inputs appear that hold key and value values respectively. We want to enter our environment variables for the API URL and access key into these new inputs like this. With that covered it's time to deploy our hard work. Hit the green deploy site button and you'll be taken to the projects dashboard to check on the build progress. Step 3. Inspect the build via the project dashboard. So here we are the project dashboard for our project and it should look something like this. Up at the top of the dashboard you'll see we have a funny looking name. Unless you're using a custom domain for the project Netlify will assign a random three part name for you which you can use to identify your projects in lists and access publicly once it's deployed. It'll be an address that looks like this. Your unique name dot netlify dot app. Note yours look different to the one I have here. The topmost area holds the preview of the deployed site once it's finished as well as the status of the current build and deploy. Notice that ours is currently set as yellow with a site deploying progress message. The other main points of interest here are the getting started section and the production deploys area. Getting started is a one time panel that shows for all new projects that you add to Netlify. Once your site is deployed successfully you'll be given the option to bring in and use a custom domain and finally to add a free SSL certificate. We'll leave the custom domain and SSL for now as they're outside the scope of this course but Netlify has some fantastic documentation on custom domains and how to use them with your projects. With production deploys you'll find a list of the most recent deploys for your project and whether they're succeeded or failed. You can inspect each separate build run to see a log of exactly what happened during the build. After a while you should see your project is finished building and then you can view your hard work in all its glory at the public URL. Bernard what's this? Red text, a warning and message about site deploy failed. Something's gone wrong with our build and the build has failed. Fortunately we can step into the specific build run and see exactly what went wrong. Looking under the production deploys area of the project dashboard you can see that we have at least one build run with a failed tag in red next to it. If we click on the most recently failed build tag to view the build log under this wall of white on black terminal text you can scroll down to see a specific point in the build where things went wrong and look for a possible solution. This is a good place to highlight the major difference between running things locally and building and deploying in a separate production environment. Locally if you run yarn build everything will build fine and you won't see any issues. However by pushing things to production deployment environment such as Netlify some sort of issues being introduced. Now the error we have here is a rather specific build environment error that's unique to Netlify. From the middle of 2020 Netlify started adding a ci = true environment variable to all builds on its platform. The build environment such as Netlify will have a list of standard internal environment variables that they make available to projects during builds but this one causes an easy to fix issue with create react app in particular. The main problem here is that this automatically injected environment variable ci = true causes the build of our project to interpret some rather innocuous warnings as real problems shutting down the build. You can read all about this specific issue on the Netlify community blog where they have an entire thread dedicated to this problem and how to solve it. Fortunately for us it's a really easy fix that we'll apply now. So step 4, exploring the build settings. Fixing our build error is quite straightforward and it only takes a simple change to the build settings but it also gives us a good opportunity to review the build settings and see what else is in there. From the project dashboard click on settings menu item in the top navigation. Once in the site settings click build and deploy link from the left hand menu. This section is where you can discover and edit all of the settings related to your site or apps build and deployment. You can change the deployment branch, environment variables, apply post processing tricks like injecting Google analytics into your site and controls who gets what notifications. For now however we're concerned with the very top box build settings. Click the edit settings button and change the build command from yarn build to ci = yarn build. Hit the save button and that's it. Such a simple fix but now Netlify will not interfere with our create react app build and you'll find things go much more smoothly from now on. The last thing is to trigger a new build and deployment run. To do that head over to the deploy section from the top navigation. This area lists out the most recent deployments as well as giving access to deployment settings, notifications and stopping automatic publishing. For our needs we're mainly concerned with the trigger deploy select element in the top right of the deployment list. Click trigger deploy and then select deploy site to kick off a new site deployment. This will add a new deployment item to the existing list and mark it as in progress. If you click into this new build you'll be able to follow along in real time as Netlify builds a project. Hopefully if everything goes according to plan you'll reach a nice blue box surrounding the message Netlify build complete. If you reach this point everything's built and will be able to go view our deployed project at our very own unique URL. Step 5 Viewing the deployed app. So back at the top of the screen click the deploys link to get back to the deploys dashboard and then take a look at the nice green URL under our project name. Mine will look different to yours but the point here is that you can click on this link to open a new screen which will have our lovely furry friends gallery mark 2 running. Take a look at the public app and take it for a spin. Select and breed from the left page through them view the dog pictures. Although we haven't built anything new in this module we've covered an awful lot of ground in understanding how building and deploy our apps in a realistic setting works. You should give yourself a huge pat on the back and go grab a well earned rest before we tackle the next module thinking in react. [ Tapping ]