Newsletter
Join the Community
Subscribe to our newsletter for the latest news and updates
Step-by-step tutorial for deploying a create-react-app React app to GitHub Pages using the gh-pages npm package.
This is a step-by-step tutorial for deploying a React app created with create-react-app to GitHub Pages, using the gh-pages npm package to publish the production build. The guide is maintained by GitHub user gitname, has over 6,900 stars on GitHub, and is translated into Traditional Chinese.
This tutorial walks through nine main steps: creating an empty GitHub repository, scaffolding a React app with npx create-react-app my-app, installing gh-pages as a development dependency, adding a homepage property to package.json, adding predeploy and deploy scripts, adding a Git remote, running npm run deploy, and configuring GitHub Pages to serve from the gh-pages branch. It also includes an optional step to push the React app's source code to the main branch. The result is a live React app accessible at a URL like https://username.github.io/repo-name.
create-react-app tool, and notes that a TypeScript version can be created with npx create-react-app my-app --template typescript.gh-pages, which pushes the contents of the build folder to a new commit on the gh-pages branch, creating the branch if it does not exist.username.github.io)."predeploy": "npm run build" and "deploy": "gh-pages -d build" to the scripts section of package.json; a custom commit message can be passed with npm run deploy -- -m "Your message".master or main and tells you to replace master with your actual branch name in the commands.create-react-app project as a free static site on GitHub Pages.gh-pages branch.username.github.io URL as a user site.gh-pages branch.gh-pages workflow to publish any static build output produced by React scripts.Running npm run deploy triggers the predeploy script, which executes npm run build and generates a distributable version of the app in a build folder. The deploy script then invokes gh-pages -d build, which pushes the folder's contents to a new commit on the gh-pages branch of the GitHub repository. Finally, in the repository's Settings → Pages area, you select "Deploy from a branch" and choose gh-pages with the root folder.
Yes, GitHub Pages is a free web hosting service provided by GitHub. The tutorial uses it with no cost, and also notes that GitHub Free users must use public repositories for Pages.
gh-pages is an npm package that publishes files to a gh-pages branch on GitHub. The tutorial installs it with npm install gh-pages --save-dev and uses it in the deploy script.
The tutorial does not cover custom domains in its main steps, but it links to a reference about preserving a CNAME file when using one.
The tutorial says to replace all occurrences of master in the commands with your actual branch name, such as main, when pushing the source code.
Yes, it explicitly mentions creating a React app with TypeScript by running npx create-react-app my-app --template typescript as a variation of the setup.
