In just 8 Steps Host your React App or Website on Github:
- Install gh-pages library as Dev Dependency: npm install -D gh-pages .
- Add these two commands to the scripts in package.json: "predeploy": "npm run build", "deploy": "gh-pages -d build".
-
Add this to the package.json:
"repository": { "type": "git", "url": "https://.../repo-name.git" } -
If you have not a github repo yet then create a new repo, add the git repo url to your local git repo.
If your local git repo already has a remote url then you have to remove it before adding the new remote url:
git remote remove origin. then add the new remote git remote add origin newURL. -
If you are using BrowserRouter then add this to the package.json:
"homepage": "github repo name". and add basename="github repo name" to the BrowserRouter element normally in App.js. - Run npm run deploy.
- Give your github username and password or access token(if you did not allow the GitHub extension).
- Go to the github repo settings, then under the Pages you can configure the settings for your React App page. Normally when you run npm run deploy it configures the github page for you and all you have to do is to copy the link for the live page...
Congratulations
You have successfully hosted your first React website on GitHub.