Deploying a React app to GitHub Pages involves a few steps. GitHub Pages allows you to host static websites directly from your GitHub repository. Here's a step-by-step guide:
If you don't have a GitHub repository for your React app, create one. Make sure your React app is ready and committed to the repository.
You'll need to install the gh-pages
package to help with deploying your React app to GitHub Pages. Open your terminal and run the following command:
bashnpm install gh-pages --save-dev
package.json
Add the following lines to your package.json
file:
json"homepage": "https://your-username.github.io/your-repository-name",
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
...
}
Replace your-username
with your GitHub username and your-repository-name
with the name of your GitHub repository.
Run the following commands in your terminal:
bashnpm run deploy
This will run the predeploy
script to build your React app and then deploy it to GitHub Pages using the deploy
script.
Go to your GitHub repository, navigate to the "Settings" tab, and scroll down to the "GitHub Pages" section. You should see a message indicating that your site is published.
Your React app is now deployed to GitHub Pages. You can access it at the URL mentioned in the "GitHub Pages" section of your repository settings.
gh-pages
package documentation for troubleshooting.Remember to update your GitHub Pages whenever you make changes to your React app by running npm run deploy
again.