gh-pages
(Optional but recommended for easy deploy)In your React + Vite project root directory:
npm install --save-dev gh-pages
vite.config.js
for GitHub PagesThis is important so your app knows the correct URL path.
// vite.config.js
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
base: '/your-repo-name/', // 👈 Replace with your repo name
plugins: [react()],
})
Example:
If your repo is portfolio-site
, then:
base: '/portfolio-site/'
package.json
ScriptsAdd these two scripts to package.json
:
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"deploy": "gh-pages -d dist"
}
Explanation:
gh-pages -d dist
tells it to deploy the dist/
folder.npm run build
This generates the dist/
folder.
npm run deploy
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/YourUsername/your-repo-name.git
git push -u origin main
gh-pages
branch.GitHub will give you a live URL:
https://YourUsername.github.io/your-repo-name/
Your React (Vite) app is now hosted on GitHub Pages 🎉
Step | Command / Action |
---|---|
Install | npm install gh-pages |
Config | Edit vite.config.js + package.json |
Build | npm run build |
Deploy | npm run deploy |
GitHub | Push code + Enable Pages |