Deployment of React Application using GitHub Pages
Last Updated :
20 May, 2024
Deploying a React application using GitHub Pages is an easy and efficient way to host your projects online for free. In this article, we will walk you through the steps to deploy your React app, making it accessible to users with a live URL.
Prerequisites
GitHub Pages is a static site hosting service that takes HTML, CSS, and JavaScript files straight from a repository on GitHub, optionally runs the files through a build process and publishes a website.
Why Use GitHub Pages for Deployment?
- Free Hosting: GitHub Pages offers free hosting for static websites, making it an ideal choice for deploying React applications.
- Easy Integration: Seamlessly integrate your GitHub repository with GitHub Pages.
- Automatic Updates: Automatically update your live site by pushing changes to your repository.
How to Deploy A React app?
Step 1. Create the GitHub repository first by doing the following steps:
Click on the + sign near the profile and click on the New Repository.
.png)
Â
Giving the name to the Repository.
.png)
Â
Clicking on the Create repository and executing the following commands which will be shown just after clicking on the create repository on your VS code terminal.
git init
git add .
git commit -m "first commit"
git branch -M main
And then enter the following command in which we have to replace the <username> with the username of your GitHub account and the <rep Name> with the name of the repository which you have created.
git remote add origin https://github.com/<username>/<rep Name>.git
For example, git remote add origin https://github.com/vishalWaghmode/TextEdit.git
And, then enter the following commands which will push the react application to the above repository
git push -u origin main
And, then refresh the GitHub site and you will get to see the uploaded code.
Step 2. Adding the GitHub Pages dependency packages
The gh-pages package allows us to publish the build file of our application into a gh-pages branch on GitHub, where we are going to host our application. Install the gh-pages dependency using npm :
npm install gh-pages --save-dev
Step 3. Adding the properties to the package.json file
The package.json file is been configured so that we can point the GitHub repository to where our react app is been deployed. The first property we have to add is at the top of the package.json file which will be given the name “homepage“, and the value for it will be in the following format:
"homepage": "https://<Username>.github.io/<Repository-name>"
Example: “homepage”: “https://vishalWaghmode.github.io/Textutil”
Then we will add “deploy” and “predeploy “properties in the script field with the following values:
"scripts":{
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
}
.png)
Â
Step 4. Pushing the code updates to the GitHub repository and finally deploying the application
For pushing the updates which we have done in the code we can use the following commands:
git add .
git commit -m "commit"
git push
And now finally deploy the application using the following command in the terminal:
npm run deploy
This command will publish your application on the branch named gh-pages and can be opened by the link given in the homepage property written in the package.json file.
Step 5. View the deployed app on GitHub
Now, to view the link for opening the application we will go on the GitHub and click on Settings then at the left of the settings we can see the code and automation where the pages field will be present, just clicking it we will get the following interface where the link will be provided.
Note: If the link does not come then just wait for 2-3 minutes and then again refresh the browser and then click on the link again on it.
Tips for Managing Your Deployment
- Automatic Deployment: Set up a GitHub Actions workflow to automate the deployment process every time you push changes to the main branch.
- Custom Domain: Use a custom domain by adding a
CNAME
file to the public
folder with your custom domain name.
- Branch Management: Ensure that the
gh-pages
branch is protected or managed properly to prevent unintended deletions or modifications.
Similar Reads
Deployment of Angular Application using Github Pages
There are various methods to deploy Angular Application such as Github Pages, Heroku, Firebase, etc. The Github provides the simplest way of all using the Github Pages. Steps to Create and Deploy Sample Angular Application to Github Pages: Install Node.js: a. Windows b. LinuxInstall Angular - CLICre
2 min read
How to Deploy Angular Application to Firebase using GitHub ?
Many of us unable to showcase our small-scale or personal projects over the web. Since hosting these projects are a bit difficult and also costs a few bucks sometimes. In this article, we will show you how to deploy your Angular application at no cost without buying any domain or hosting provider. A
3 min read
How to Deploy React App on Netlify Using Github?
React App is a web or mobile application that is built using the React library of JavaScript. It helps users create interactive, single-page, and dynamic applications. These applications are built using HTML, CSS, and JavaScript. Deployment of a React app can be done via GitHub, Netlify, or any othe
6 min read
How do you deploy a React application?
Deploying a React application is the process of making your React project accessible on the internet so that users can interact with it. It involves several steps to ensure your application is properly built, uploaded to a hosting service, and configured to work correctly in a live environment. This
2 min read
How to Add Custom Domain To GitHub Pages?
Adding a custom domain to your GitHub Pages site can significantly enhance its professional appearance and make it easier for your audience to remember and access. This guide will walk you through the process step-by-step, ensuring you can easily configure your custom domain and optimize your site's
2 min read
Deploying Node.js Applications
Deploying a NodeJS application can be a smooth process with the right tools and strategies. This article will guide you through the basics of deploying NodeJS applications. To show how to deploy a NodeJS app, we are first going to create a sample application for a better understanding of the process
5 min read
How to manage global state in a React application?
Global state refers to data that is accessible across multiple components in a React application. Unlike the local state, which is confined to a single component, the global state can be accessed and modified from anywhere in the component tree. In this article, we will explore the following approac
7 min read
React application that provides Country Information
Country Information is a web application to get the country's information using ReactJS. This project basically implements functional components and manages states. The user can insert the country name in the search input field, and after clicking the search button, this application makes an API cal
5 min read
How To Implement Code Splitting in Redux Applications?
Code splitting in Redux applications refers to the practice of breaking down the application into smaller, manageable chunks that can be loaded on demand. This helps in reducing the initial load time of the application by only loading the necessary parts of the application when needed. Dynamic impor
3 min read
How to Clone a Project From GitHub using VSCode?
Cloning a project from GitHub is often the first step for developers looking to contribute to open-source projects or work collaboratively with their team. While there are various ways to clone a GitHub repository, using Visual Studio Code (VSCode) adds a layer of convenience and integration. In thi
2 min read