So, today we'll see how to host our react-app in GitHub pages for free.
Pre-requisites
- GitHub account
- Project created in react js
- Node installed
- Code Editor [VS Code preferred]
Steps
First of all, install node in your system.
Navigate to your project root directory & open that in VS Code. First check whether node is installed in your system or not. Open New Terminal & put the command
npm --version
.
- Install the
gh-pages
npm package using following command -npm install gh-pages --save-dev
- Now, login to your GitHub account & create a new repo with any name. I'm using demo-react-host for mine.
- Navigate to VS Code now & open package.json file to edit the homepage property. The homepage's value should be in the format
https://{username}.github.io/{repo-name}
- Add deployement scripts to the same file.
"scripts": {
+ "predeploy": "npm run build",
+ "deploy": "gh-pages -d build",
"start": "react-scripts start",
"build": "react-scripts build",
},
- Add the remote to local git repo by putting following command
git remote add origin https://github.com/{username}/{repo-name}.git
- Upload all the files to GitHub repo.
- Deploy the site to GitHub repo using the command
npm run deploy
- Now, navigate to the link given in the "homepage" property of package.json. You must see the homepage you've created.
In case you're getting error while doing remote origin
follow below:
- Just put following command
git remote rename origin upstream
; You can change name of upstream to any name[upstream1, upstream2 etc].
- Then add new remote origin by putting following command
git remote add origin https://username.github.io/repo_name
- Then, put the following command to push all commits
git push -u origin --all
- Now, deploy the site using following command
npm run deploy
It should result in status as 'Published' at the end.
- Now, navigate to the link given in the "homepage" property of package.json. You must see the homepage you've created.
I've tried to explain how to host your react app in GitHub pages for free in the simplest manner possible. However, if you're facing any difficulty, do mention it in the comment. Will try to help.
You can follow me on LinkedIn, Instagram or check my latest projects on my GitHub. Also, you can check my portfolio too.
[Keep Coding] [Thanks]
Top comments (0)