Create a React app with Vite and deploy it on GitHub

badr21

Badreddine Boudaoud

Posted on March 26, 2023

Create a React app with Vite and deploy it on GitHub

Introduction

Deploying a React app on GitHub can be a great way to share your application with others and make it available online. In this article, we will walk through the steps required to create a React app with Vite and deploy it on GitHub.

React is a popular open-source front-end JavaScript library, used for building user interfaces for web applications quickly and efficiently with significantly less code than you would with vanilla JavaScript.It was developed by Facebook and is now maintained by Facebook and a community of individual developers and companies.

Vite is a build tool and development server that is used to build modern web applications. It is designed to be fast, lightweight, and easy to use. Vite is developed by Evan You, the creator of Vue.js, and is heavily inspired by the development experience provided by Vue.js.

GitHub is a web-based platform that provides a wide range of collaborative tools for software development teams. It is a popular platform for hosting, sharing, and collaborating on code repositories.

Prerequisites

To follow along with this tutorial, you will need:

1- Node.js installed on your system Node >= 16.

2- A GitHub account.

When developing a React app, one important step is to deploy the project to a hosting service so that others can access it. GitHub is a popular platform for hosting and sharing code, and it also provides a simple way to deploy React projects using ‘gh-Pages’.‘gh-pages’ is a package for Node.js that allows you to easily deploy a static website to GitHub Pages.

With this being said, let’s begin the tutorial.

Create a GitHub repository

The first step is to create a GitHub repository for your project. If you already have a repository for your project, you can skip this step. To create a new repository, follow these steps:

1- Go to GitHub.com and log in to your account.

GitHub log in page

2- Click on the “+” icon in the top right corner and select “New repository”.

GitHub repository options

3- Give your repository a name, and add a brief description if desired. I will name it react-project.

Create new repository page, add the repository name

4- Choose whether to make the repository public or private.

Create new repository page, choose public or private

5- Click on the “Create repository” button.

Create new repository page, click create repository

6- After clicking on Create repository you will see this page.

The page after creating new repository

Create a React app with Vite

If you have not yet created a React app, you can use Vite. With Vite, you can create a React app with less build time and the files in the folder are minimal with optimal output. To do this, you will need to have Node.js installed on your computer. Here are the steps:

1- Create a folder and name it whatever you want.

Create a folder

2- Open the folder with your preferred IDE, I will be using Visual Studio Code.

Open folder with visual studio code

3- Make sure to open your terminal in that folder.

Open terminal in visual studio code

4- To install Vite, you need to run:

npm create vite@latest
Enter fullscreen mode Exit fullscreen mode

In the terminal, run npm create vite@latest

5- After running npm create vite@latest, you will be asked to put a project name.

In the terminal, write the project name

I will name it react-project.

In the terminal, the project is named react-project

6- You will be asked to select a framework. Since this project is a React project, you need to select React from the list.

In the terminal, select React from the list

7- You will also be asked to choose a variant. I will choose JavaScript.

In the terminal, choose a variant

8- Change your directory to the react-project by running:

cd <Your project name>
Enter fullscreen mode Exit fullscreen mode

In the terminal, run cd <your project name>

In my example, I will run:

cd react-project
Enter fullscreen mode Exit fullscreen mode

9- To install all the dependencies React needs run:

npm install
Enter fullscreen mode Exit fullscreen mode

In the terminal, run npm install

10- Wait for the installation to finish.

In the terminal, wait for the installation to finish

Install the gh-pages package

After the dependencies are installed completely, you need to install ‘gh-Pages’ package.

1- To install ‘gh-pages’ run:

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

In the terminal, run npm install — save-dev gh-pages

2- Wait for the installation to finish.

In the terminal, wait for the installation to finish

Update the package.json file

The ‘gh-pages’ package uses the homepage field in the package.json file to determine where to deploy your app. To update the package.json file, follow these steps:

1- Add a homepage field with the URL of your GitHub repository in the following format:

“homepage”: https://.github.io//

Update the package.json file

2- In the scripts tag add:

“predeploy”: “npm run build”,

“deploy”: “gh-pages -d dist”.

Update the package.json file

The “predeploy” and “deploy” scripts are used to automate the deployment process.

3- Save the changes.

Update the vite.config.js file

1- Add base: “//”, to vite.config.js, in my example it would be base: “/react-project/”

Update vite.config.js file

2- save the changes.

Push the React app to GitHub

With the ‘gh-Pages’ package installed and the package.json and the vite.config.js files updated

1- Initialize a Git repository by running the command:

git init
Enter fullscreen mode Exit fullscreen mode

In the terminal, Initialize a Git repository

This will create a new Git repository in your project’s directory.

2- Add all the files in your project to the Git repository by running the command:

git add .
Enter fullscreen mode Exit fullscreen mode

In the terminal, Add all the files in your project to the Git repository

This will stage all the files in your project for committing.

3- Commit the changes by running the command:

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

In the terminal, Commit the changes

This will create a new commit in the Git repository with the message “Initial commit”.

4- Rename the master branch to main by running the command:

git branch -M main
Enter fullscreen mode Exit fullscreen mode

In the terminal, Rename the master branch to main

5- Add the remote repository by running the command:

git remote add origin <repository URL>
Enter fullscreen mode Exit fullscreen mode

The GitHub repository page

This is the repository URL in the GitHub repository that you’ve created

In the terminal, add the remote repository

This will add the GitHub repository as a remote for your local Git repository.

6- Finally, push the changes to the remote repository by running the command:

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

In the terminal, push the changes to the remote repository

This will push the initial commit of your local Git repository to the GitHub repository.

Deploy your React app

With the React app pushed to Github, you can now deploy your React app.
1- Run the command:

npm run deploy
Enter fullscreen mode Exit fullscreen mode

In the terminal, deploy the React app

2- Wait for the deployment to finish.

In the terminal, the deployment is finished

Access your app

Once the deployment is complete, you can access your React app by going to the URL specified in the homepage field of your package.json file. Typically, the URL will be in the form of https://.github.io//.

Or you can find the URL by clicking on the repository setting.

The GitHub repository setting page

Then click on pages.

In the GitHub repository page click on pages

Here is the URL.

The GitHub repository setting pages page

Click on the URL, and here is your deployed React app.

The project page

Conclusion

Deploying a React app to GitHub Pages using the ‘gh-Pages’ package is a simple and convenient way to share your work with others. By following the steps outlined in this article, you can easily deploy your own React projects to GitHub Pages and share your work with the world.

💖 💪 🙅 🚩
badr21
Badreddine Boudaoud

Posted on March 26, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related