DEV Community

Akshay Kurhekar
Akshay Kurhekar

Posted on

2

How to deploy React App on GitHub for free

To deploy React App we need to follow five simple steps

Alt Text

Step 1

Let's create react app as name of my app is react_app.

$ npx create-react-app react_app
Enter fullscreen mode Exit fullscreen mode

Step 2

Install the gh-pages package as a "dev-dependency" of the app.

$ npm install gh-pages --save-dev

           OR

$ yarn add gh-pages 
Enter fullscreen mode Exit fullscreen mode

Step 3

Create new repository on GitHub as Public.

The commands shown in the following steps can all be issued from within the app's folder. Add some properties to the app's package.json file.

At the top level, add a homepage property. Define its value to be the string http://{username}.github.io/{repo-name}, where username is your GitHub username, and repo-name is the name of the GitHub repository.

Since my GitHub username is git_user_name and the name of my GitHub repository is react_app, I added the following property:

//...
"name": "Project Name",
"homepage": "http://git_user_name.github.io/react_app",
"version": "0.1.0...",

Enter fullscreen mode Exit fullscreen mode

In the existing scripts property, add a predeploy property and a deploy property, each having the values shown below:

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

          OR

"scripts": {
   //...
   "predeploy": "yarn build",
   "deploy": "gh-pages -d build"
  }
Enter fullscreen mode Exit fullscreen mode

Step 4

Create a git repository in the app's folder.

$ git init
Enter fullscreen mode Exit fullscreen mode

Initialized and Add the GitHub repository as a "remote" in your local git repository.

$ git remote add origin https://github.com/git_user_name/react-app.git
Enter fullscreen mode Exit fullscreen mode

This will make it so the gh-pages package knows where you want it to deploy your app.

Step 5

It will also make it so git knows where you want it to push your source code (i.e. the commits on your master branch).

Generate a production build of your app, and deploy it to GitHub Pages.

$ npm run deploy
    OR
$ yarn deploy    
Enter fullscreen mode Exit fullscreen mode

That's it! Your app is now accessible at the URL you specified in step 4. In my case, my app is now accessible at: https://git_user_name.github.io/react-app/

I recommend exploring the GitHub repository at this point. When I explored it, I noticed that, although a master branch did not exist, a gh-pages branch did exist. I noticed the latter contained the built app code, as opposed to the app's source code. Optionally, commit your source code to the "master" branch and push your commit to GitHub.

$ git add .
$ git commit -m "Create a React app and publish it to GitHub Pages"
$ git push origin master

Enter fullscreen mode Exit fullscreen mode

I recommend exploring the GitHub repository once again at this point. When I did that, I noticed that a master branch now existed, and it contained the app's source code. So, the master branch held the source code, and the gh-pages branch held the built app code.

Now it's time to check hosted app.

Alt Text

You can see now in the Environments section github-pages is active.
after clicking it will redirect to this.

Alt Text

Now, Click on view Deployment Button to check hosted app.

πŸ₯³ Happy hosting 😍 !

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong Β· web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video 🌢️πŸ”₯

Top comments (2)

Collapse
 
akshaykurhekar profile image
Akshay Kurhekar β€’ β€’ Edited

If you find this post helpfull, pls share your feedback.

Collapse
 
akshaykurhekar profile image
Akshay Kurhekar β€’

simple blog to deploy react app on git hub

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay