How to Deploy a React App to GitHub Pages
Sh Raj

Sh Raj @sh20raj

About: IamSh

Location:
🔆💖 INDIA💖🔆
Joined:
Jan 9, 2022

How to Deploy a React App to GitHub Pages

Publish Date: Jul 2 '24
47 11

How to Deploy a React App to GitHub Pages

Deploying a React app to GitHub Pages is a great way to share your project with the world. GitHub Pages is a free hosting service that makes it easy to publish static websites directly from your GitHub repository. This article will guide you through the steps to deploy your React app to GitHub Pages.

Prerequisites

Before we begin, make sure you have the following:

  1. Node.js and npm: Install Node.js and npm from nodejs.org.
  2. Git: Install Git from git-scm.com.
  3. GitHub Account: Create a GitHub account if you don't have one already.

Step 1: Create a React App

If you haven't already created a React app, you can do so using Create React App. Open your terminal and run the following commands:

npx create-react-app my-react-app
cd my-react-app
Enter fullscreen mode Exit fullscreen mode

Replace my-react-app with the name of your project.

Step 2: Install gh-pages Package

To deploy your React app to GitHub Pages, you'll need to use the gh-pages package. Install it by running:

npm install gh-pages --save-dev
Enter fullscreen mode Exit fullscreen mode

Step 3: Update package.json

Add the following properties to your package.json file:

  1. Homepage: Add a homepage field to specify the URL where your app will be hosted. The URL should be in the format https://<username>.github.io/<repository-name>/.
   "homepage": "https://<username>.github.io/<repository-name>/"
Enter fullscreen mode Exit fullscreen mode

Replace <username> with your GitHub username and <repository-name> with the name of your repository.

  1. Predeploy and Deploy Scripts: Add predeploy and deploy scripts to automate the deployment process.
   "scripts": {
     "predeploy": "npm run build",
     "deploy": "gh-pages -d build"
   }
Enter fullscreen mode Exit fullscreen mode

Your package.json should now look something like this:

{
  "name": "my-react-app",
  "version": "0.1.0",
  "private": true,
  "homepage": "https://<username>.github.io/<repository-name>/",
  "dependencies": {
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build"
  },
  "devDependencies": {
    "gh-pages": "^3.2.3"
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Initialize a Git Repository

If your project is not already a Git repository, initialize it by running:

git init
git remote add origin https://github.com/<username>/<repository-name>.git
Enter fullscreen mode Exit fullscreen mode

Replace <username> and <repository-name> with your GitHub username and repository name.

Step 5: Commit Your Changes

Add and commit your changes:

git add .
git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode

Step 6: Push to GitHub

Push your project to GitHub:

git push -u origin master
Enter fullscreen mode Exit fullscreen mode

Step 7: Deploy to GitHub Pages

Deploy your app by running:

npm run deploy
Enter fullscreen mode Exit fullscreen mode

This command will build your React app and push the build directory to the gh-pages branch of your repository. GitHub Pages will then serve the files from this branch.

Step 8: Access Your Deployed App

After the deployment is complete, you can access your app at https://<username>.github.io/<repository-name>/. It might take a few minutes for the changes to be reflected.

Conclusion

Deploying a React app to GitHub Pages is a straightforward process that allows you to share your projects with ease. By following these steps, you can quickly get your app online and accessible to others. Happy coding!

Comments 11 total

  • Hòa Nguyễn Coder
    Hòa Nguyễn CoderJul 2, 2024

    thanks pro

    • Sh Raj
      Sh RajJul 2, 2024

      Thanks for the appreciation brother 😊

  • Hemang Yadav
    Hemang YadavJul 2, 2024

    Nice Blog

    • Sh Raj
      Sh RajJul 2, 2024

      Thanks for the appreciations bro ☺️

  • Jorge
    Jorge Jul 2, 2024

    Not fully sure but I think GH will removed the gh-pages branch approach in favor of GH Action

    • Sh Raj
      Sh RajJul 2, 2024

      Yep, GitHub Action is a far better approach

    • Nils Diekmann
      Nils DiekmannJul 2, 2024

      But for now the 'gh-pages branch' is the default configuration

  • skywarth
    skywarthJul 2, 2024

    Or just simply use this package and be done with it :D Far more simple:
    dev.to/skywarth/deploying-your-vit...

    • Sh Raj
      Sh RajJul 2, 2024

      Nice Creation

  • Elana Carlson
    Elana CarlsonJul 2, 2024

    My name is Christopher Gonzalez, Riverside, Califonia. My story of how I won the Powerball lottery of $768.4M is a bit of a tale. I was feeling very lucky that day because I had contacted Dr. Eze Odogw to help me with the winning Powerball numbers.You can reach him via ezeodogwuspellhome@gmail.com and you can also call or Whats-app him at +2347018157305 GOODLUCK!!

  • WTP | WhatThePortal.com
    WTP | WhatThePortal.comJul 6, 2024

    Always nice to see more support for GitHub Pages - they're an under-appreciated goldmine.

Add comment