Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

c a
c a

Posted on • Originally published atchrisdevcode.hashnode.dev on

Deploying A React app on GitHub Pages

GitHub offers more than just a host for your code. In this short tutorial, I will walk you through deploying a static react app/project onGitHub Pages.

I will be using one of my React projects from GitHub:

https://github.com/ChrisAchinga/myRepos

A programmer's learning tool is by practicing --I said that...

Let's Get Started:

This tutorial assumes you already have your react project setup and ready to deploy

Step 1: Install the Dependencies (gh-pages):

I use npm for my projects, so while in your project root directory, open the project on your terminal or cmd (windows).

Install thegh-pages package as a dev-dependency of the app

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

Step 2: Define Homepage in package.json

In thepackage.json file in your react app and add homepage property using the given syntax:

http://{username}.github.io/{repo-name}
Enter fullscreen modeExit fullscreen mode

Where {username} is your GitHub username, and {repo-name} is the name of the GitHub repository. Below is an example for my project:

"homepage": "http://ChrisAchinga.github.io/myRepos",{  "name": "myrepos",  "homepage": "http://ChrisAchinga.github.io/myRepos",  "version": "0.1.0",  "private": true,}
Enter fullscreen modeExit fullscreen mode

Step 3: Deploy script inpackage.json file

Now we can add the deploy script in the package.json file. In the existing scripts property, add a predeploy property and a deploy property, each having the values shown below:

"scripts": {  // some code before  "predeploy": "npm run build",  "deploy": "gh-pages -d build"}
Enter fullscreen modeExit fullscreen mode

So your "scripts" should look like this:

{  "scripts": {    "start": "react-scripts start",    "predeploy": "npm run build",    "deploy": "gh-pages -d build",    "build": "react-scripts build",    "test": "react-scripts test",    "eject": "react-scripts eject"  },}
Enter fullscreen modeExit fullscreen mode

Step 4: Deploy Your App

Update your GitHub repository using git commands:

npm run deploy
Enter fullscreen modeExit fullscreen mode

Step 5: Commit and Push to GitHub

On your project terminal, run the deploy script

git add .git commit -m "gh-pages deploy"git push
Enter fullscreen modeExit fullscreen mode

Kudos your React app is ready for view... on the URL:

https://{github-user-name}.github.io/{repo-name}/
Enter fullscreen modeExit fullscreen mode

https://chrisachinga.github.io/myRepos/

Deployment Preview

Resources For GitHub Pages

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Python(Django). JS. Software Eng
  • Location
    Kenya
  • Education
    Software Engineering
  • Work
    Software Eng Lead
  • Joined

More fromc a

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp