0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【React】React AppをGitHub Pagesでデプロイする(2021年9月)

Posted at

はじめに

Reactで作成したWebアプリをGitHub Pagesでデプロイする際に結構躓いたので,こうすると出来たよという現時点での情報を備忘録として残す。

環境

  • Windows

完成品

https://fk-ln.github.io/react-test/
とりあえずデプロイの手順だけを示すので,使うリポジトリはnpx create-react-appで作成したデフォルトのサンプル。

手順

  1. GitHubでリポジトリを作成してmain(master)リポジトリにpush
  2. ローカルリポジトリでnpm install gh-pages --save-dev
  3. package.jsonを開いて以下を追記(//Scripts/の二か所)
{ 
  ~省略~

  "homepage": "https://fk-ln.github.io/tdu-new-school/",
 ※GitHubの`Settings`→`Pages`に表示されるURLをコピペ

  ~省略~

 "Scripts": {

    ~省略~

    deploy": "npm run build && gh-pages -d build",
  }

  ~省略~
}
  1. npm run buildを実行
  2. GitHubのSettingsPagesでBranchをgh-pages,フォルダを/(root)に設定

これで先ほど設定した(package.jsonに書いた)URLにアクセスするとアプリが使えるはず。(反映に数分かかるかも)

gh-pages

gh-pagesというnpmパッケージではソースコードをコンパイルしてbuildフォルダを作成し,それをgh-pagesブランチにpushしてくれる。

躓いた点

  1. package.json"homepage": を書いていなかった
     →どうやらデプロイ先のURLは明示的に示す必要があるっぽい
  2. 操作にWindows Powershellを使っていた
     →npm run buildではGitHubにpushする作業が行われるのでgit bashなどのgitコマンドを使える環境で行う。
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?