* created using create-react-app
In this tutorial, I'll show you how I created a React app, then deployed that React app to GitHub Pages.
To create the React app, I used the create-react-app
npm package. To deploy the React app to GitHub Pages, I used the gh-pages
npm package.
You can visit the deployed React app, here: https://gitname.github.io/react-gh-pages
This repository consists of two branches:
master
- this branch contains the React app's source code.gh-pages
- this branch contains the React app built from that source code.
-
Node.js
andnpm
are installed. Here are the versions I used while writing this tutorial:$ node --version v16.13.2 $ npm --version 8.1.2
Note: During the
Node.js
installation process, I opted to installnpm
automatically. Installingnpm
adds both thenpm
andnpx
commands to my system. -
Git is installed. Here's the version I used while writing this tutorial:
$ git --version git version 2.29.1.windows.1
Note: I will be using the command-line Git client in this tutorial.
-
A GitHub account.
-
In this step, I'll use a form on GitHub to create an empty repository named
react-gh-pages
in my GitHub account.By "empty," I mean one that contains no files.
To do that, I'll sign into my GitHub account, then fill out the following form:
When filling out the form, I'll enter the name I want the repository to have, which is
react-gh-pages
.In this tutorial, I'll be using a repository named
react-gh-pages
. In case you're using a repository that has a different name, you can replace all occurrences ofreact-gh-pages
in these instructions with the name of the repository you're using.I want the repository to be empty. So, in the "Initialize this repository with" section of the form, I'll leave all the checkboxes empty so GitHub doesn't initialize the repository with any files (i.e.
README.md
,.gitignore
, orLICENSE
).At this point, my GitHub account contains an empty repository named
react-gh-pages
.
-
In this step, I'll use
create-react-app
to create a React app namedmy-app
.$ npx create-react-app my-app
That command will create a React app written in JavaScript. To create one written in TypeScript, you can issue this command instead:
$ npx create-react-app my-app --template typescript
Either command will create a new folder named
my-app
, which will contain the newly-created React app.I'll enter the new folder.
$ cd my-app
All of the remaining commands shown in this tutorial can be run from that new folder.
Foreshadowing: In addition to containing the files that make up the React app, that new folder is also a Git repository. I will utilize that characteristic in step 6.
-
In this step, I'll install the
gh-pages
package in a way that also designates it as a development dependency of the React app.$ npm install gh-pages --save-dev
That command both (a) installs the
gh-pages
package and (b) adds its name and version number to thedevDependencies
section of thepackage.json
file.
-
In this step, I'll add a
homepage
property to thepackage.json
file.To edit the file, I'll use a code editor such as vi or Visual Studio Code.
$ vi package.json
The value of the
homepage
property is the URL at which I want people to be able to access the React app.Since I'll be deploying the React app as a GitHub project page, I'll use this URL format:
https://{username}.github.io/{repo-name}
Here's what the resulting property looks like:
{ "name": "my-app", "version": "0.1.0", + "homepage": "https://gitname.github.io/react-gh-pages", "private": true,
That URL format will allow me to deploy the React app as a GitHub project page. If, instead, I wanted to deploy the React app as a GitHub user page and/or I wanted it to have a custom domain name, I could do so by using one of the URL formats shown in the official
create-react-app
GitHub Pages deployment guide.
-
In this step, I'll add some deployment scripts to the
package.json
file.I'll add these two scripts to the
scripts
object:"scripts": { + "predeploy": "npm run build", + "deploy": "gh-pages -d build", "start": "react-scripts start", "build": "react-scripts build",
That
deploy
script will allow me to deploy the React app as a GitHub project page. If, instead, I wanted to deploy the React app as a GitHub user page (regardless of domain name), I could do so by using thedeploy
script shown in the officialcreate-react-app
GitHub Pages deployment guide.
-
In this step, I'll add a remote to the local repository.
That remote will be named
origin
and will point to the repository I created in step 1. The URL format is:https://github.com/{username}/{repo-name}.git
$ git remote add origin https://github.com/gitname/react-gh-pages.git
That will make it so that, when I (or the
gh-pages
npm package acting on my behalf) perform agit push
, Git knows where it can send the files being pushed.
-
In this step, I will do two things:
- Build a distributable instance of the React app
- Deploy that distributable instance of the React app to GitHub
To do both of those things, I'll issue this one command:
$ npm run deploy
That will cause the
predeploy
anddeploy
scripts defined inpackage.json
to run.Under the hood, the
predeploy
script will build a distributable version of the React app and store it in a folder namedbuild
. Then, thedeploy
script will push the contents of that folder to thegh-pages
branch of the GitHub repository.GitHub Pages will automatically detect that files have been pushed to the
gh-pages
branch of the GitHub repository. Once it detects that, it will begin serving those files — in this case, the distributable version of the React app — to anyone that visits the URL I specified in step 4; i.e. the app's base URL.That's it! The React app is now accessible at its base URL: https://gitname.github.io/react-gh-pages 🚀
-
In the previous step, the
gh-pages
npm package pushed the distributable version of the React app to a branch namedgh-pages
in the GitHub repository.In this step, I will push the source code of the React app to a branch named
master
in the GitHub repository.To do that, I'll issue the following commands:
$ git add . $ git commit -m "Create a React app and deploy it to GitHub Pages" $ git push origin master
I recommend exploring the GitHub repository at this point. It will have two branches:
master
andgh-pages
. Themaster
branch will contain the React app's source code, while thegh-pages
branch will contain the distributable version of the React app.
- Special thanks to GitHub (the company) for providing us with the GitHub Pages hosting service for free.
- And now, time to turn the default React app generated by
create-react-app
into something unique!