DEV Community

Cover image for How to deploy React App to GitHub Pages
Ibrahim Ragab
Ibrahim Ragab

Posted on • Edited on

How to deploy React App to GitHub Pages

Did you build React app and want to deploy it, following these simple steps you able to deploy and showing the world your amazing app.

I will show how to create and deploy React App using create-react-app and GitHub Pages

Prerequisites :

Make sure you have Node.js and Npm installed in your machine.

Notice You’ll need to have Node 8.10.0 or later on your local machine.

Procedure :

1- First create a repository named my-app using create-react-app.

npm init react-app my-app

2- We need to install GitHub Pages package as a dev-dependency.

cd my-app
npm install gh-pages --save-dev

3- Add properties to package.json file.

The first property we need to add at the top level homepage second we will define this as a string and the value will be "http://{username}.github.io/{repo-name}" {username} is your GitHub username, and {repo-name} is the name of the GitHub repository you created it will look like this :

"homepage": "http://yuribenjamin.github.io/my-app"

Second in the existing scripts property we to need to add predeploy and deploy.

"scripts": {
//...
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
}

package.json example

4- Create a Github repository and initialize it and add it as a remote in your local git repository.

Now, create a remote GitHub repository with your app name and go back initialize this
git init
add it as remote
git remote add origin git@github.com:Yuribenjamin/my-app.git

5- Now deploy it to GitHub Pages.

just run the following command :

npm run deploy

successful build

this command will create a branch named gh-pages this branch host your app, and homepage property you created in package.json file hold your link for a live preview, or you can open the branch setting scroll down to GitHub Pages section you will find this:
Your site is puplished

Visit deployed app

6- commit and push your commit to GitHub. Optionally

git add .
git commit -m "Your awesome message"
git push origin master

Recap

We created React App using create-react-app
then we installed gh-pages as a dev-dependency
and in package.json file we added some properties homepage also in existing scripts property we added predeploy and deploy
and created a remote repository and initialize it
and run npm run deploy to generate a production build and deploy it to GitHub Pages.

if you have any questions, please don't hesitate to ask.
Always happy to hear from you.

Oldest comments (252)

Collapse
 
apol0x profile image
Apol0x

Nice introductiong man. Saved it ;)

Collapse
 
yuribenjamin profile image
Ibrahim Ragab

Thanks :)

Collapse
 
deven96 profile image
Diretnan Domnan

Nice article ... love your github page even more

Collapse
 
yuribenjamin profile image
Ibrahim Ragab

Thanks mate :)

Collapse
 
mhkamel profile image
MHKamel

Nice article ... love your github page even more

Collapse
 
yuribenjamin profile image
Ibrahim Ragab

Thanks Mohamed :)

Collapse
 
johnmutina profile image
JohnMutina

Thank you for this, Ibrahim! Cheers

Collapse
 
yuribenjamin profile image
Ibrahim Ragab

You are welcome John Cheers :)

Collapse
 
kseniyaco profile image
KseniyaCo

Hello. I followed all the instructions, but when I enter npm run deploy I recieve "The "file" argument must be of type string. Received type undefined" error. How to handle this? Thank you

Collapse
 
yuribenjamin profile image
Ibrahim Ragab

Can you double Check the package.json file make sure you set correctly the homepage, predeploy, deploy check commas single quotes or double, if not provide me your repo url and make sure I will help

Collapse
 
seamanb2k profile image
DigitalEmma

found this post useful, thank you...I have a question, after i have pushed my react app to github page, when i visit the url it displays blank with no error message shown, any pointers why i'm seeing this happen/
plus this is my first project in react.
Thank you

Collapse
 
yuribenjamin profile image
Ibrahim Ragab

Thanks, double check your process, if the same error occurred please share with me your github repo and I will help immediately

Collapse
 
anabeatrizzz profile image
Ana Beatriz

I did the changes but a blank page is displayed yet.

Collapse
 
nidhipal09 profile image
Nidhi pal

Hey, thanks for this very helpful tutorial. I am also getting a blank page. I did what was suggested (changing the router and link path as the github repo name) but still its not resolved.

repo link - github.com/Nidhipal09/covid-19-pro...
websited - nidhipal09.github.io/covid-19-proj...

can you pls help

Collapse
 
piruthuvi22 profile image
Piruthuvi

I got this same problem in the beginning. If you follow this tutorial correctly,then try this one.

Your home page route should be like this





<Route exact path="/your github repo name">



instead for

and in your rendered component change the




<Link to="/your github repo name">



instead for




<Link to="/">



And another important point you should consider. your github repo name must be valid name for example dots or space should not be in your repo name because that things conflict with URL.
Collapse
 
learndeepak profile image
Learn-Deepak

Thanks a lot @Piruthuvi.

Collapse
 
ultrax007 profile image
Rajat Sawarkar

this should be mentioned in the create-react-app docs, was so frustrated because of this simple code change, thanks alot mate @Piruthuvi

Collapse
 
layersofabstraction profile image
Jordan Nash

Thank you. I had to follow every step and re-watch your no-nonsense video for this to work from the beginning but it was actually more helpful than React's documentation for deploying SPAs.

Collapse
 
yuribenjamin profile image
Ibrahim Ragab

Thanks! I will rework this soon with more detailed steps also enhance video

Collapse
 
zacastaylor profile image
Zac Taylor

Hey Ibrahim, my app won't deploy. I went over your instruction twice but am still having issues. Would you mind taking a look at my repo, it's github.com/ZacASTaylor/zacastaylor.... Thanks!

Collapse
 
yuribenjamin profile image
Ibrahim Ragab

Hello Zac

you did well but you need to make some changes to make your project run online kindly follow this process carefully:

1 - you need to change repo name because of dots in repo name conflict with URL, zacastaylor it's good enough and works with me.

2- in package.json change homepage with new value should be "zacastaylor.github.io/zacastaylor"

2.1- change predeploy to the new value "npm run build", yarn is fine but it's not tested.

3- in App.js change line 25 to be <Route exact path='/zacastaylor'

4- in component => layout => Header.js line 37 to be <Link to="/zacastaylor"

download the project and change the repo name, after that remove the old connection you can check git remote -v you will see this connected with old repo remove connection by git remote rm this command will take one parameter the full command will be git remote rm origin.

if you check again git remote -v will display nothing to your terminal after that do the change above and make a commit with new changes then push and run npm run deploy.

happy to hear from you Zac, kindly update me if this process success or fail

Collapse
 
zacastaylor profile image
Zac Taylor

Thanks so much for you help Ibrahim, but the issue was that I needed slightly different instructions because I am trying to have my React app hosted on my GitHub user page and not a project page. I followed the instructions here: dev.to/javascripterika/deploy-a-re...

Thanks again!

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
pak11273 profile image
Isaac Pak

video is private...

Collapse
 
marcmdion profile image
marcmdion

Hi Ibrahim, How would you update a react app that you have deployed via GitHub? Thanks, Marc

Collapse
 
yuribenjamin profile image
Ibrahim Ragab

Hello Mark I hope you're doing well, if you want to updating to new releases check this link below
create-react-app.dev/docs/updating...

Collapse
 
yuribenjamin profile image
Ibrahim Ragab

Yeah thanks ming 😁👍

Some comments may only be visible to logged-in visitors. Sign in to view all comments.