How to Deploy a Vite React App to GitHub Pages in 2024

Posted by


Vite is a modern build tool that includes hot module replacement (HMR), fast builds, and much more. In this tutorial, I will guide you through deploying a Vite React app to GitHub Pages. This will allow you to showcase your project to the world with ease.

Before we start, make sure you have the following prerequisites:

  1. Node.js and npm installed on your machine.
  2. Basic knowledge of React and GitHub.
  3. A GitHub account.

Let’s get started!

Step 1: Create a Vite React app
First, let’s create a new Vite React app using the following command:

npm init @vitejs/app my-react-app --template react

Replace my-react-app with the name of your app. This command will create a new directory with all the necessary files for your Vite React app.

Step 2: Set up GitHub repository
Next, navigate to your GitHub account and create a new repository for your Vite React app. Make sure to copy the repository link as we will need it in the next steps.

Step 3: Configure deployment settings
To deploy your app to GitHub Pages, we need to configure the deployment settings in the package.json file. Add the following lines to the scripts section:

"scripts": {
  "predeploy": "npm run build",
  "deploy": "gh-pages -d dist",
}

This will build your app before deploying it to GitHub Pages using the gh-pages package.

Step 4: Install necessary packages
Run the following command to install the required packages:

npm install gh-pages --save-dev

This will install the gh-pages package, which we will use to deploy our app to GitHub Pages.

Step 5: Update vite.config.js
In the vite.config.js file, add the following lines to specify the base URL for your app:

export default defineConfig({
  base: '/my-react-app/',
  plugins: [react()]
})

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

Step 6: Deploy your app
To deploy your Vite React app to GitHub Pages, run the following command:

npm run deploy

This command will build your app and deploy it to GitHub Pages using the settings we configured earlier.

Step 7: Check your deployed app
Finally, navigate to https://<your-github-username>.github.io/my-react-app/ to see your deployed Vite React app in action.

Congratulations! You have successfully deployed your Vite React app to GitHub Pages. You can now share your project with others and showcase your skills as a developer.

0 0 votes
Article Rating

Leave a Reply

38 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@VladyslavDihtiarenko
1 day ago

If you use React Router in your project, you're probably facing an issue with empty pages (blank screen with no errors in the console) or 404 not found errors for your assets. Check out this video, I've covered those issues there: https://www.youtube.com/watch?v=uStf2HMXcAs

@paulocavalcanti3808
1 day ago

If I choose gh-pages as the Branch, I can execute the app, but cannot access the doc. On the other hand, if I choose main as the Branch, I can see the doc, but cannot execute the app.

@hikaru_e
1 day ago

This is one of the most concise tutorials I’ve ever seen. Thank you for valuing our time!

@wavingtoearth
1 day ago

Hey I followed your tutorial and deployed the app successfully.

But I got an error. So I made some changes to my code and when I am trying to push the code to github with the general
β€œgit push -u origin main” command, it shows a message β€œeverything is up-to-date” but the repository isn’t really updated on github.

Can you help me about it?

@cz6609
1 day ago

Absolute lifesaver. Thank you!

@Plantarc
1 day ago

Very helpful tutorial, thank you so much

@BLINKBOXHD
1 day ago

I don't see gh-pages as an option under branch

@DeepakSharma-cf5cz
1 day ago

thank alot bro ❀‍πŸ”₯❀‍πŸ”₯
you gained 1 more subscriber

@ChideraAbaraonye
1 day ago

Thanks a lot. I deployed my first React app, thanks to this video.

@sarveshkulkarni6287
1 day ago

faltu bakwas video hai ,time waste ,sala actions me kuch nai ata

@yannanicolas3700
1 day ago

I did all the steps but then when i go to Actions, there is no failed deployment that i can re-run, please help me. Ty!

@muhammedyaseen1404
1 day ago

Thanks man, i spent a so much time figuring it out

@am_horrorfinder
1 day ago

guys, this guy is absolute straight to point guy. I wondered this is new channel owner. thanks man. love from INDIA.

@b__yag
1 day ago

mine is error : Build

ENOENT: no such file or directory, open '/home/runner/work/tictactoe-game/tictactoe-game/package-lock.json'

im already re run but display error message like above text

@dmitry9463
1 day ago

How to make it automatically redeploy after pushing new changes to the repo? Because I've added some changes, pushed them to the repo, but nothing has changed on the page by public link.

@amansemwal9514
1 day ago

hey im not getting the gh-pages option, ive tried multiple times.
its showing deployed, but only a blank screen shows up

@isuskrist7416
1 day ago

Nice video. What VSCode theme are you using?

@Rao_vishal321
1 day ago

It helps me a lot thanks ❀

@mrxmart3238
1 day ago

net::ERR_ABORTED 404 (Not Found)

@reiyua
1 day ago

Worked for me! Cheers you legend!!

38
0
Would love your thoughts, please comment.x
()
x