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:
- Node.js and npm installed on your machine.
- Basic knowledge of React and GitHub.
- 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.
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
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.
This is one of the most concise tutorials I’ve ever seen. Thank you for valuing our time!
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?
Absolute lifesaver. Thank you!
Very helpful tutorial, thank you so much
I don't see gh-pages as an option under branch
thank alot bro ❤🔥❤🔥
you gained 1 more subscriber
Thanks a lot. I deployed my first React app, thanks to this video.
faltu bakwas video hai ,time waste ,sala actions me kuch nai ata
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!
Thanks man, i spent a so much time figuring it out
guys, this guy is absolute straight to point guy. I wondered this is new channel owner. thanks man. love from INDIA.
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
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.
hey im not getting the gh-pages option, ive tried multiple times.
its showing deployed, but only a blank screen shows up
Nice video. What VSCode theme are you using?
It helps me a lot thanks ❤
net::ERR_ABORTED 404 (Not Found)
Worked for me! Cheers you legend!!