loading...
Cover image for How to deploy React App to GitHub Pages

How to deploy React App to GitHub Pages

yuribenjamin profile image Ibrahim Ragab Updated on ・2 min read

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

Posted on by:

yuribenjamin profile

Ibrahim Ragab

@yuribenjamin

Frontend developer @WuiltApp, Former Lawyer

Discussion

markdown guide
 

Very interesting tutorial Ibrahim!
I have a question: how do you update the production build?
I mean: after deploying the app on GitHub, I realized that I need to change some stuffs. So I have generated new commits. If my app points to the master branch, how can I reflect that new changes on the production build then?

Thanks

 

Make changes commit then deploy again, that's it

 
 

so i commit and push to my origin/github master, then i run "npm run deploy" again?

 

Hi! Thanks for the tutorial!

I accidentally copy pasted this on my terminal : git remote add origin git@github.com:Yuribenjamin/my-app.git

and now i keep getting this:
Cloning into 'node_modules/gh-pages/.cache/git@github.com!Yuribenjamin!my-app.git'...
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! my-app@0.1.0 deploy: gh-pages -d build
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the my-app@0.1.0 deploy script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /Users/TrishaBernice/.npm/_logs/2020-06-15T16_41_28_880Z-debug.log

I tried git remote rm origin and it still won't work. can you please advice me on what i should do here? I will really appreciate it!

 

No problem
git remote - v
Will display origin and the wrong remote url
To delete it
git remote rm origin
Then check again
git remote - v
Will display nothing time to add the right one
Happy Hacking

 

Hi Ibrahim, Thanks for the fast reply!

I did this:
Trishas-MBP-2:src TrishaBernice$ git remote rm origin
Trishas-MBP-2:src TrishaBernice$ git remote -v
Trishas-MBP-2:src TrishaBernice$ git remote add origin git@github.com:trishabernice/portfolio.git
Trishas-MBP-2:src TrishaBernice$ npm run deploy

and I still got this...
Cloning into 'node_modules/gh-pages/.cache/git@github.com!Yuribenjamin!my-app.git'...
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! my-app@0.1.0 deploy: gh-pages -d build
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the my-app@0.1.0 deploy script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /Users/TrishaBernice/.npm/_logs/2020-06-15T17_01_16_913Z-debug.log

Is there anything else I can try to fix the issue?

Earlier I did this:
Trishas-MBP-2:src TrishaBernice$ git remote add origin github.com/trishabernice/portfolio...
and i did git remote rm origin and then added the one for ssh and I still got the same error.

Do you have any suggestions for what I can do? I'll really appreciate it!

  1. go to github.com and create Repositories => your-app
  2. create your personal access token github.com/settings/tokens
  3. git remote set-url origin username:yourtoken@github.com/user...
  4. npm run deploy

or

  1. yarn add gh-pages
  2. yarn deploy

mejimaru's solution worked if anyone ran into the error that @trishabernicer faced.

Thanks @mejimaru

 

I'd the same problem and then read a lot of blogs/articles I find the solution. I'd changed the git in my PATH in this question you can find the solution then open the a new terminal and run the command npm run deploy I hope this help you.

Regards.

 

Hello ! This was a big help ! However, at step 5, I received this error:

"Cloning into 'node_modules/gh-pages/.cache/git@github.com!idongessien!idngessnio.git'...
Warning: Permanently added the RSA host key for IP address '140.82.114.4' to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! idngessnio@0.1.0 deploy: gh-pages -d build
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the idngessnio@0.1.0 deploy script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /Users/idongessien/.npm/_logs/2020-05-17T18_23_28_805Z-debug.log"

 
 

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

 

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.
 
 

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

 

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!

 

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

 

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!

 
[deleted]
 
 

Hello, Ibrahim.
I have such problem:
fatal: A branch named 'gh-pages' already exists.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! movie@0.1.0 deploy: gh-pages -d build
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the movie@0.1.0 deploy script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Can you help me? Here is my repo github.com/pitysek16/Movies-React-app

 

hii katerina check last commit in this repo
github.com/Yuribenjamin/Movies-Rea...
and already your app running
yuribenjamin.github.io/Movies-Reac...

if you fix this tell me to delete repo above.

 
 

Please what did you do to resolve it. I'm having the same issue.

when you receive error " A branch named 'gh-pages' already exists.", you have to delete the .config sub-folder in gh-pages folder in node_module.

 

How would I add changes (such as changing the header, font, color, etc) to the page after deploying? I committed and pushed and it successfully shows on my github repo but when I go to my live link none of my changes show.

My repo: github.com/kimberlybone/my-portfol...
My live site: kimberlybone.github.io/my-portfoli...

 

did you try to delete GH-page branch && build directory, and deploying the app again? if not try this

 

Hey - does this mean, everytime you update something on the page, we need to Delete Gh-page branch && delete build directory folder, and basically deploy again?

Is there no automatic update happening? (i.e. like, when deployed on netlify or smhtg.) ? Thanks!

to update run npm run deploy after each update

as you said committing and deploying works fine but you have to wait for the change to take an effect on the live link ( and sometimes the repo itself)

 

Great article. I got it to work publishing the react app. Note that for the homepage I used "https" rather than "http". Also since I used yarn for my packages I tried to use it for the deploy as well, but it didn't work. I got it to work with npm however. Thanks again.

 

Hello, i am doing the routing for my website, but only the landing page shows up. Could you have a look? I think the problem should be in index.js, main.js, App.js. Thanks!!
tianyiz8.github.io/personal-website/
github.com/tianyiz8/personal-website

 

Hey thanks for your comment, handling route in react can easly done by using react router package, not by default way as i saw in your app, also read about Single Page App, and declaritive programming
github.com/ReactTraining/react-router
Happy Hacking!

 

Hello! Could you be more specific? I did use react-router-dom, and where are declaritive programming and Single Page App? I still have no idea how to make it work.
Thanks!

 

Hi! At step 5, I added SSH keys however I received such error:
Cloning into 'node_modules/gh-pages/.cache/git@github.com!Spring-dot!face-detector.git'...
ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! face-detector@0.1.0 deploy: gh-pages -d build
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the face-detector@0.1.0 deploy script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /home/akanksha/.npm/_logs/2020-05-21T06_16_57_148Z-debug.log
Please help!! I am a beginner!

 
 

Hi. I am having issues deploying my React app to github pages. Could you take a look?
github.com/LKian/ToDoList

I added homepage to package.json, along with
"predeploy": "npm run build",
"deploy": "gh-pages -d build"

I added the gh-pages as a dev dependency

The error seems to occur when I run "npm run deploy"

todolist@0.1.0 build /mnt/c/Users/Admin/Desktop/Code/todolist
react-scripts build

internal/modules/cjs/loader.js:638
throw err;
^

Error: Cannot find module 'react-dev-utils/crossSpawn'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
.
.
.
.

Thank you!

 

hello Leah
give it try again I cloned your repo and walked through my tutorial,
and everything working fine with me check ...
dev-to-uploads.s3.amazonaws.com/i/...
dev-to-uploads.s3.amazonaws.com/i/...
dev-to-uploads.s3.amazonaws.com/i/...

 

Oh thanks for the screenshots. The only difference I noticed was that for some reason even though I installed "gh-pages" yesterday, it wasn't there. I re-installed and gh-pages now appears in package.json.

I then tried to npm run deploy and am still getting an error. Any other thoughts as to what is wrong?

Creating an optimized production build...
Failed to compile.

./src/index.js
Error: [BABEL] /mnt/c/Users/Admin/Desktop/Code/todolist/src/index.js: Cannot find module '@babel/core' (While processing: "/mnt/c/Users/Admin/Desktop/Code/todolist/node_modules/babel-preset-react-app/index.js")

I read that the gh-pages is supposed to create a new branch but that is not happening. I'm going to try to delete the repo and start from scratch. After I make a new repo, should I still be running the following setup commands?
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:LKian/ToDoList.git
git push -u origin master

and THEN running npm run deploy? Still getting an error

Update: I deleted node_modules and package-lock.json and npm installed again. And it says published!!!!!!!!!! Still not showing up on the live page but I know sometimes it takes a while. Will keep you updated :-)

lkian.github.io/ToDoList/index.html

Got it. Apparently you have to add "/index.html" to the path

no, just the URL that equal to the home page key in the package.json
lkian.github.io/ToDoList
dev-to-uploads.s3.amazonaws.com/i/...

 

Hi, I have this error:
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

 

can you provide your github repo

 

in this tutorial, I introduced deploying react App to Github pages, I see your app didn't consume react packages, so you can deploy your app using the normal process by open settings navigate to GitHub Pages then in Source choose the master branch if you want run your app from the master branch.
happy hacking!

Sorry, can you specify a bit? I need to push it in separate repository inside /andrii1.github.io?

what I need to say your app not a react app so you don't need this tutorial to deploy it to gihtub you can deploy it from the setting

So, I have my React app in separate folder my-app, when I'm trying to upload it to GitHub, I see this:
dev-to-uploads.s3.amazonaws.com/i/...

Update: issue was solved with:
git push -f origin master

But, still andrii1.github.io/my-app is not live..
github.com/andrii1/my-app

 

Hi Ibrahim, I have recently built my first react project. Despite having followed all your instructions on the thread, my app won't deploy. Would appreciate it if you could take a look at it for me:

ian1202.github.io/react-project/

Thank you!

 

Not all the instruction I think, do this changes to package dot json, change home to homepage and add the right value "http://{username}.github.io/{repo-name}" . Solved

 

It worked! Sorry for having missed that detail. Thank you!

Your are welcome, Alawys happy to help 🙂

 

Helloooo :)
So, I am begginer... and I did what you "said"...and it worked... although I have two issues that I dont understand.

  • that is supposed to be my project page - irenkaaa.github.io/sweet-shop-proj... ... when u go is showing my 'not found page'... but when u click on the header parts, it is working - but with changed address - irenkaaa.github.io/{home?store?login ..etc.}..and it is only working the views... my node.js server is not exactly on there..
  • also the other thing... after the deploying , i got that build folder.. and basically...all my files changed and i caouldnt working on the project..Idk how but I managed to return them and now I can work, but I am totally lost...
 

first issue check routing, second cuz you watch gh-branch change to master
happy hacking!

 

Thanks a lot! On my way to check the stuff!!!

Keep safe from Covid!

 

I deployed my react app to GitHub and its working but not what I expected. When I run the app on the localhost its works perfectly and I can see all the pictures and all the navigation works. After deploy on Github, I can not see anymore the images which were a relative path link to my app and also navigation back button seems not working. Any hints on how to overcome these issues. Here is react app Github deployed Link- Your site is published at mjachowdhury.github.io/mjacportfolio/

would you please look into this

 

hey mjach your problem-specifically in routing there's a difference between routing on dev server and the production double-check this point I know you can do it, and if not sadly contact me and I will fix this for you

 

I followed the instructions and got the same messages from the terminal up to right under the "gh-pages -d build" and then I get these lines

gh-pages -d build

bash: /dev/tty: No such device or address
error: failed to execute prompt script (exit code 1)
fatal: could not read Username for 'github.com': Invalid argument

my repo is github.com/Kriviears/React-Portfolio

 

hey Kriviears, first you should fix all bugs before deploy, second check this repo this is your app running successfully, and double-check the fix commit after you fix this problem notify me to delete you repo = >
yuribenjamin.github.io/React-Portf...
github.com/Yuribenjamin/React-Port...

 

Lovely. Thank you for this.

If you're having the following error:
Host key verification failed.
fatal: Could not read from remote repository.

Remove your remote:
git remote remove origin

Then create a new remote. no marks:
git remote add ht tps://github.com/username/repo-name.git

Using the git@github.com:... syntax ends up using SSH to clone, and inside the container, your private key is not available.

 

Hi Ibrahim, First Thank you for a such a wonderful and helping blog.I have one doubt that i was able to succesfully deploy my react app but when i make changes locally i am unable to get those changes on my react app. Can you please guide how to do that in a proper way?

 

Yeah make changes commit and run the deploy again to generate fresh build your terminal will tell you the rest,
Another way delete build delete gh branch make changes rub deploy
Happy Hacking

 

What is wrong with mine, i do set up the ssh key. but i still got this error.

Cloning into 'node_modules\gh-pages.cache\git@github.com!DevChrisDev!ChrisJoshuaManuel.git'...
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! chris-resume@0.1.0 deploy: gh-pages -d build
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the chris-resume@0.1.0 deploy script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Client\AppData\Roaming\npm-cache_logs\2020-06-20T00_08_24_598Z-debug.log

all help would be much appreciated.

 

Great Post! Thank you for sharing it !
Is it possible to deploy it when you have API Keys in your code?

Normally when we send our code to git, we put the files with API keys or secrets in the .gitignore file and use the Git hub only as a repository. I want to deploy a simple app that has an API in it. Any ideas?

Thanks a lot!

 

Urw welcome bruno, since we're working with react-app you can check
create-react-app.dev/docs/adding-c...

 

Hello, Ibrahim! Thank you for your tutorial. I was just wondering what changes would be necessary if I wanted to deploy the react app directly to the homepage (eg.: username.github.io), if that's even possible.

 
 

In case anyone else runs into the same issue that I had. When running the build command for the first time, I got error:

fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.

Creating a new branch gh-pages manually before running the deploy fixed this.

 

Hello Ibrahim. I followed your tutorial and I am trying to deploy my first app to github pages. I keep getting this error when I run "npm run deploy".

gh-pages -d build

Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Any help please?

 

make sure that you provide the right credentials

 

Hello Ibrahim, my app deploys now, but it is showing only HTML plain webpage. Would you mind looking at my Github repo, and guide me any necessary changes to my project? github.com/eaindra-wp/portfolio

 

hey Eaindra can you make master is the default, not Github pages to be able to clone your repo.
and put "homepage": "xyz....", at the top of your module
and check console errors, make sure console is free!

 

Hello Ibrahim, I have updated the steps that you suggested, but it seems like my react-app cannot find the CSS files yet.

 

This does not work for me. Did everything exactly as you wrote but I get this error :

git-upload-pack '.': git-upload-pack: command not found
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

 

After I deploy the application and I make some change but I can't update the change, how can I do that? I use git push -u origin master but not updating the changes. It's updating only again npm run deploy but it's load all file again, I need only update specific changes.

 

Delete github branch and build file make your changes commit then deploy easy peasy

 

I gave it a try, but I'm getting a 404 error. Any idea what it can be?

repo: github.com/gsus0194/gsus0194.githu...
gitpage: gsus0194.github.io/professional_we...

Thanks

 

hi bro, give your app and repo simple name without any special characters kindly check this commit for fix this bug github.com/Yuribenjamin/gsus/commi...
and your app is running successfully
yuribenjamin.github.io/gsus/
finally, after fixing this issue tell me to delete your repo

 
 

When I added the homepage property in package.json file and then after that, I did "npm start" then it is also showing blank page.
After adding homepage:: localhost:3000/Portfolio
Before adding the homepage:: localhost:3000/

What to do, please guide me.

 

man this gh-pages package is pure magic. Super cool! Took all my suffer of deploying to gh pages. Before it I used to do stuff like track build folder into master branch and do a subtree to gh-pages branch. Like this:

git push origin --delete gh-pages &&
git branch -D gh-pages &&
git subtree split --prefix build -b gh-pages

 

I'm having problem in deploying my react app on github pages.
After following the instructions and publishing it, when going to the link a blank page is showing up.
When inspecting via react developer options it is showing the following error
dev-to-uploads.s3.amazonaws.com/i/...