React App*
* created using create-react-app
Introduction
In this tutorial, I'll show you how I deployed a React app—which I created using create-react-app
to GitHub Pages.
You can visit the deployed app, at https://cenacrharsh.github.io/test/.
This repository contains the files related to the app. The master
branch contains the app's source code (the code the app's developers edit), and the gh-pages
branch contains a built version of the app (i.e. the code that GitHub Pages serves to the app's visitors).
The remainder of this document contains a tutorial on creating a React app (using create-react-app
) and deploying that app to GitHub Pages.
Tutorial
Prerequisites
-
An adequate version of
Node.js
is installed. Here's the adequate version I use:
$ node --version v14.17.0
-
An adequate version of
npm
is installed. Here's the adequate version I use:
$ npm --version 6.14.13
-
An adequate version of
create-react-app
is installed. Here's the adequate version I use:
$ create-react-app --version 4.0.3
In the case of
create-react-app
, you can either install it globally (i.e.$ npm install -g create-react-app
) or install it locally (i.e.$ npm install create-react-app
). If you choose the latter, you will have to specify its path whenever you invoke it (e.g.path/to/node_modules/.bin/create-react-app
). A GitHub account.
A command-line Git client setup according to GitHub.
Procedure
-
Create an empty repository on GitHub.
For this tutorial, I'll create a repository named
test
.
By empty, I mean without aREADME.md
file, a.gitignore
file, aLICENSE
file, or any other files. -
Create a new React app on your computer.
$ create-react-app test
This is the app you will deploy to GitHub Pages in step 7.
I opted to give the app the same name as my GitHub repository (i.e.test
). However, you can name them differently from one another (e.g. you can name your appapp-123
and your GitHub Repositoryrepo-456
).
This will create a new folder namedtest
(or whatever you named your app) on your computer. Install the
gh-pages
package as a "dev-dependency" of the app.
The commands shown in the following steps can all be issued from within the app's folder, so make sure to navigate to the newly created
test
folder and then use the commands. Use$ cd test
to navigate to the app folder from where you are currently on the terminal.
After navigating to the test
folder use:
```
$ npm install gh-pages --save-dev
```
-
Add some properties to the app's
package.json
file.At the top level, add a
homepage
property. Define its value to be the stringhttp://{username}.github.io/{repo-name}
, where{username}
is your GitHub username, and{repo-name}
is the name of the GitHub repository you created in step 1. Since my GitHub username iscenacrharsh
and the name of my GitHub repository istest
, I added the following property:
//... "homepage": "http://cenacrharsh.github.io/test"
In the existing
scripts
property, add apredeploy
property and adeploy
property, each having the values shown below:
"scripts": { //... "predeploy": "npm run build", "deploy": "gh-pages -d build" }
-
Create a git repository in the app's folder.
$ git init
-
Add the GitHub repository as a "remote" in your local git repository.
$ git remote add origin https://github.com/cenacrharsh/test.git
This will make it so the
gh-pages
package knows where you want it to deploy your app in step 7.
It will also make it so git knows where you want it to push your source code (i.e. the commits on yourmaster
branch) in step 8. -
Generate a production build of your app, and deploy it to GitHub Pages.
$ npm run deploy
That's it! Your app is now accessible at the URL you specified in step 4.
In my case, my app is now accessible at: https://cenacrharsh.github.io/test/
Explore the GitHub repository at this point. You will see that, although amaster
branch did not exist, agh-pages
branch did exist. Thegh-pages
branch contained the built app code, as opposed to the app's source code. -
Optionally, commit your source code to the "master" branch and push your commit to GitHub.
$ git add . $ git commit -m "Create a React app and publish it to GitHub Pages" $ git push -u origin master
Explore the GitHub repository once again at this point.You will notice that a
master
branch now existed, and it contained the app's source code.
So, themaster
branch holds the source code, and thegh-pages
branch holds the built app code.
Top comments (6)
Try it soon
yup
Nice article, have you thought about using using a GitHub action to deploy
Sure, I will learn how to deploy using GitHub action.
isn't the same process for deploying any repo??
package.json is unique to react apps hence changes made are not necessary elsewhere.