📑
「Github Page」にReactを公開する方法
1)Reactプロジェクトを作成
npx create-react-app app-dir
2)Branchの発行し、GitHubのリポジトリを作成
VSCodeの場合「Branchの発行」をクリックしリポジトリ名を入力する
3)Reactアプリのビルド
npm run build
→buildディレクトリが作成されます
4)gh-pagesをインストール
Reactアプリを簡単にデプロイできる「gh-pages」ツールをインストール
npm install --save gh-pages
5)package.jsonを編集
▽package.jsonにて「homepageフィールド」の追加、「デプロイスクリプト」の追加
package.json
{
"homepage": "https://<GitHubアカウント名>.github.io/<GitHubリポジトリ名>/",
"name": "app-dir",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
6)「npm run deploy」を実行する
cd app-dir
npm run deploy
公開URLについて
"homepage": "https://<GitHubアカウント名>.github.io/<GitHubリポジトリ名>/",
「Branchの発行」後に作成されるリポジトリにてGitHubでBranchを「Save」をクリックすると数分後URLが表示されます
▽数分後URLが表示されます
▽サイトにて表示確認できます
参考サイト
Discussion