In this blog, we will see how to host a React.js website on Github pages.
Prerequisite:
- Should have GitHub account. If not, then create one.
Follow the below steps to deploy your website to Github pages:
- Open your project in VS code or any other editor as per your choice.
- Open Terminal. In VS code, Go to Terminal→New Terminal.
- Push your code to Github. Ignore this step if already Done.
- Open your package.json file and add one line.
- "homepage": "https:///USERNAME.github.io/REPO_NAME",''
- After adding the above line, your code should look like:
{ "homepage": "https:///USERNAME.github.io/REPO_NAME", "name": "your-project", "version": "0.1.0", "private": true, // Rest of your code- where USERNAME= Your github username.
- REPO_NAME= Your repository name which you want to deploy to github pages.
- Add the below to your scripts:
"predeploy": "npm run build", "deploy": "gh-pages -d build",- Your scripts should looks like:
"scripts": { "predeploy": "npm run build", "deploy": "gh-pages -d build", "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" },
- Install gh-pages package. Run the below command in terminal:
npm install gh-pages --save-dev
- Run in terminal: npm run deploy
- New branch will be created with name: gh-pages
- Go to GitHub Pages settings. Settings→Code and automation→Pages
- Configure the "Build and deployment" settings as shown below:
- Source: Deploy from a branch
- Branch:
- Branch:
gh-pages - Folder:
/ (root)
- Branch:
- Click Save.
- Your React.js code will be deployed to Github pages. Check by clicking the URL provided by Github pages.