How to deploy React App to GitHub pages?

Deploying a React App to GitHub Pages

Introduction

In this article, we will guide you through the process of deploying a React app to GitHub Pages. GitHub Pages is a free service provided by GitHub that allows you to host static websites and applications. It’s a great way to deploy your React app quickly and easily, without having to worry about setting up a server or managing dependencies.

Step 1: Create a New React App

Before we can deploy our React app to GitHub Pages, we need to create a new React app. We can do this using the create-react-app command-line tool. Here’s how:

  • Open your terminal and run the following command:
    npx create-react-app my-app

    This will create a new React app called my-app in the current directory.

Step 2: Install the Required Packages

We need to install the gh-pages package to deploy our React app to GitHub Pages. Run the following command:

npm install gh-pages

This will install the gh-pages package and its dependencies.

Step 3: Configure the package.json File

In the package.json file, we need to add the gh-pages package as a dev dependency. Here’s how:

  • Open the package.json file and add the following line:
    "devDependencies": {
    "gh-pages": "^10.1.1"
    }

    Step 4: Create a New GitHub Pages Site

To deploy our React app to GitHub Pages, we need to create a new site. Here’s how:

  • Go to your GitHub repository and click on the "Settings" icon in the top right corner.
  • Click on "GitHub Pages" from the left menu.
  • Select the branch you want to deploy to (e.g. main).
  • Click on "Create a new page" and select "New file".
  • Name the file index.html and add the following code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>My App</title>
    </head>
    <body>
    <h1>Welcome to my app!</h1>
    </body>
    </html>
  • Click on "Save" to save the file.

Step 5: Configure the index.html File

In the index.html file, we need to add a script tag to include the index.js file from our React app. Here’s how:

  • Add the following line to the top of the file:
    <script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
  • Add the following line to the bottom of the file:
    <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>

    Step 6: Add the index.js File

In the index.js file, we need to import the App component from our React app. Here’s how:

  • Add the following line to the top of the file:
    import React from 'react';
  • Add the following line to the bottom of the file:
    import App from './App';

    Step 7: Deploy the App to GitHub Pages

To deploy the app to GitHub Pages, we need to create a new commit and push it to the remote repository. Here’s how:

  • Create a new commit by running the following command:
    git add .
    git commit -m "Deploy to GitHub Pages"
  • Push the commit to the remote repository by running the following command:
    git push origin main

    Step 8: Verify the Deployment

To verify that the app is deployed to GitHub Pages, we need to check the repository settings. Here’s how:

  • Go to your GitHub repository and click on the "Settings" icon in the top right corner.
  • Click on "GitHub Pages" from the left menu.
  • Verify that the "Deploy to GitHub Pages" option is enabled.

Tips and Variations

  • You can customize the index.html file by adding your own HTML, CSS, and JavaScript files.
  • You can also add a README.md file to the repository to provide more information about your app.
  • If you want to deploy multiple React apps to GitHub Pages, you can create a separate branch for each app and deploy them separately.
  • You can also use a gh-pages alias to simplify the deployment process.

Conclusion

Deploying a React app to GitHub Pages is a straightforward process that requires minimal setup and configuration. By following these steps, you can quickly and easily deploy your React app to a live GitHub Pages site. Remember to customize the index.html file and add any necessary files to your repository to ensure a smooth deployment process.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top