Deploying and Hosting a React App
Deploying a React app has become quite an easy task these days. With modern build commands packaged with tools like create-react-app, and amazing hosting platforms like Netlify and Surge, you can quickly build and deploy your React app with a single command or push to Github.
Building a React app
First, we need to prepare our React app to run on production. When youâre working on the app locally on your machine, youâre working in development mode. This allows you to easily see your source code for debugging. We want our users to see the minified version of the app, which is smaller and faster. We can create this âproduction buildâ by running a build command.
If youâre using create-react-app, the build command is npm run build
or yarn build
. Your entire production app will then be built inside the build
directory in your project. If youâre using gatsby, it will appear in the public
directory. This directory is important because itâs what we need to tell our deployment tool where to host.
Deploying with Surge
After youâve ran the build command, you can deploy the app in seconds using a deploy tool called Surge.sh.
- To install surge, just run
npm install --global surge
. - Then simply run
surge
. If the command was installed, you should now receive some prompts: - First, enter in
[your-project-directory]/build
. - Then, type the name of your subdomain that you want to deploy on or leave the existing one as is.
- Confirm the prompt and thatâs it! Your app is now deployed on [your-sub-domain].surge.sh
While Surge is great, it is limiting for serious projects. For one, it cannot build directly from your Github repository. They offer git hooks, but this is only to setup a pre-commit hook to run whenever you push from your local machine. Building locally isnât always the most convenient option. Thatâs why we recommend a more robust tool that can build on with its own servers at high performance.
Deploying React apps on Netlify
Netlify offers the perfect platform for âcontinuous deploymentâ, a term used to describe the process of rapidly building and releasing code after a build has succeeded. Sure, you could build and deploy on your local machine, but then you may want to additionally push your code to Github. This process isnât great because you donât know what code is deployed vs what code is on Github.
Netlify allows you to quickly setup a build and deploy pipeline right from a Github repo. After pushing to Github, youâll see a build appear in Netlify with the status and terminal output. If it fails, youâll get notified why and see the errors right from the Netlify dashboard.
- First, login to Netlify and click create a new site from Git:
- Next, select Github:
- If you don't see your repository, follow this link:
- Choose your repository once authenticated in Github:
- Setup your build settings (this is assuming you're using create-react-app, if you're using Gatsby, it's the public directory):
- Click deploy site!
- You should now see your site URL:
- Now, whenever you push to the Github
master
branch, you'll see a build appear inside Netlify:
Netlify's starter plan is free for hosting your React projects (up to a certain build time and bandwith).
Hope this worked out! If you had issues, Netlify has a great community forum where you can search for issues.
đ React School creates templates and video courses for building beautiful apps with React. Download our free Material UI template.