最近Ruby on Railsのチュートリアル読んで手を動かしてる。
ところどころコケたところかつ覚えているところだけ誰か用にメモしておきます。
1.5.2 Herokuにデプロイする の節
$ git push remote heroku master
を実行したところ、以下のエラー
Counting objects: 115, done. Compressing objects: 100% (97/97), done. Writing objects: 100% (115/115), 53.82 KiB | 2.07 MiB/s, done. Total 115 (delta 10), reused 0 (delta 0) remote: Compressing source files... done. remote: Building source: remote: ~(略)~ remote: Compiling… remote: Compilation failed: remote: error Command "webpack" not found. remote: remote: yarn run v1.22.17 remote: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. remote: remote: remote: ! remote: ! Precompiling assets failed. remote: ! remote: ! Push rejected, failed to compile Ruby app. remote: remote: ! Push failed remote: Verifying deploy... remote: remote: ! Push rejected to ***. remote: To https://git.heroku.com/***.git ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'https://git.heroku.com/***.git'
gitにpushしようとしてエラーが出てしまう。
調査
remote: ! remote: ! Precompiling assets failed. remote: !
にある通り、コンパイル前にコケているということがわかります。 直前のログには
remote: Compiling… remote: Compilation failed: remote: error Command "webpack" not found.
とあるように、webpackがないからコケているのが明白です。(他の人の症状も見たけど、configファイルに書いた設定で文字処理がうまくいきませんでしたというパターンもある模様。きちんとログを読むのが大事ですね)
ただし、ここで素直にwebpackをyarnからインストールしてはいけない。仔細は省略するwebpack周りが本質的な問題ではなかったからです。 (筆者はここからnode_modulesを削除したり@rails/webpacker等を変更したりと色々と検討したが、結局1時間ほど無駄に潰した)
解決策
Gemfileの構成を以下の通りに修正した。ほとんど公式ドキュメントからの転用+pg周りの記載。 (ちなみにpgとはPostgreSQL RDBMSにアクセスするために必要なライブラリ)
source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } gem 'rails', '6.0.4' gem 'puma', '4.3.6' gem 'sass-rails', '5.1.0' gem 'webpacker', '4.0.7' gem 'turbolinks', '5.2.0' gem 'jbuilder', '2.9.1' gem 'bootsnap', '1.10.3', require: false group :development, :test do gem 'sqlite3', '1.4.1' gem 'byebug', '11.0.1', platforms: [:mri, :mingw, :x64_mingw] end group :development do gem 'web-console', '4.0.1' gem 'listen', '3.1.5' gem 'spring', '2.1.0' gem 'spring-watcher-listen', '2.0.1' end group :test do gem 'capybara', '3.28.0' gem 'selenium-webdriver', '3.142.4' gem 'webdrivers', '4.1.2' end group :production do gem 'pg', '1.1.4' end gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
こちらでpushすると問題なくherokuに反映された。