0

環境

rails 6.0.2.1
ruby 2.6.3
amazon linux
mysql
nvm v14.2.0
node v14.2.0
yarn 1.22.4

前提・実現したいこと

EC2を使ってAWSにRails6で作ったアプリをデプロイしているところです。
https://qiita.com/Yuki_Nagaoka/items/55ed1610cfc1f59398b1
上記の記事を参考にデプロイまで行いました。

発生している問題・エラーメッセージ

デプロイが完了した段階で、アプリのURLにアクセスしたところ
We're sorry, but something went wrong.
のエラー画面が表示されました。

原因を探ったところ、
(アプリ名)/log/production.rb
に以下のエラーメッセージが残されていました。

[3aa04024-9cfc-47d6-8bb5-be5607993cd6] ActionView::Template::Error (Webpacker can't find application.js in /var/www/rails/portgram/public/packs/manifest.json. Possible causes:
1. You want to set webpacker.yml value of compile to true for your environment
   unless you are using the `webpack -w` or the webpack-dev-server.
2. webpack has not yet re-run to reflect updates.
3. You have misconfigured Webpacker's config/webpacker.yml file.
4. Your webpack configuration is not creating a manifest.
Your manifest contains:
{
}
):
[3aa04024-9cfc-47d6-8bb5-be5607993cd6]     4:   <%= csp_meta_tag %>
[3aa04024-9cfc-47d6-8bb5-be5607993cd6]     5:   <%= stylesheet_link_tag 'custom', media: 'all', 'data-turbolinks-track': 'reload' %>
[3aa04024-9cfc-47d6-8bb5-be5607993cd6]     6:   <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
[3aa04024-9cfc-47d6-8bb5-be5607993cd6]     7:   <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
[3aa04024-9cfc-47d6-8bb5-be5607993cd6]     8:   <%= render 'layouts/shim' %>
[3aa04024-9cfc-47d6-8bb5-be5607993cd6]     9: </head>
[3aa04024-9cfc-47d6-8bb5-be5607993cd6]   
[3aa04024-9cfc-47d6-8bb5-be5607993cd6] app/views/layouts/_head.html.erb:7
[3aa04024-9cfc-47d6-8bb5-be5607993cd6] app/views/layouts/application.html.erb:3

該当のソースコード

production.rb

Rails.application.configure do
  config.cache_classes = true

  config.eager_load = true

  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

  config.assets.compile = true

  config.active_storage.service = :local

  config.force_ssl = true

  config.log_level = :debug

  config.log_tags = [ :request_id ]

  config.action_mailer.perform_caching = false

  config.i18n.fallbacks = true

  config.active_support.deprecation = :notify

  config.log_formatter = ::Logger::Formatter.new

  if ENV["RAILS_LOG_TO_STDOUT"].present?
    logger           = ActiveSupport::Logger.new(STDOUT)
    logger.formatter = config.log_formatter
    config.logger    = ActiveSupport::TaggedLogging.new(logger)
  end

  config.active_record.dump_schema_after_migration = false

  ActiveRecord::Middleware::DatabaseSelector::Resolver
  ActiveRecord::Middleware::DatabaseSelector::Resolver::Session
end

webpacker.yml

default: &default
  source_path: app/javascript
  source_entry_path: packs
  public_root_path: public
  public_output_path: packs
  cache_path: tmp/cache/webpacker
  check_yarn_integrity: false
  webpack_compile_output: true

  resolved_paths: []

  cache_manifest: false

  extract_css: false

  static_assets_extensions:
    - .jpg
    - .jpeg
    - .png
    - .gif
    - .tiff
    - .ico
    - .svg
    - .eot
    - .otf
    - .ttf
    - .woff
    - .woff2

  extensions:
    - .mjs
    - .js
    - .sass
    - .scss
    - .css
    - .module.sass
    - .module.scss
    - .module.css
    - .png
    - .svg
    - .gif
    - .jpeg
    - .jpg

development:
  <<: *default
  compile: true

  check_yarn_integrity: true

  dev_server:
    https: false
    host: localhost
    port: 3035
    public: localhost:3035
    hmr: false
    # Inline should be set to true if using HMR
    inline: true
    overlay: true
    compress: true
    disable_host_check: true
    use_local_ip: false
    quiet: false
    pretty: false
    headers:
      'Access-Control-Allow-Origin': '*'
    watch_options:
      ignored: '**/node_modules/**'

test:
  <<: *default
  compile: true

  public_output_path: packs-test

production:
  <<: *default

  # Production depends on precompilation of packs prior to booting for performance.
  compile: false

  # Extract and emit a css file
  extract_css: true

  # Cache manifest.json for performance
  cache_manifest: true

試したこと

・webpackerをec2にインストール
・下記記事を参照、実行
https://github.com/rails/webpacker/issues/2478
https://qiita.com/natecotus/items/a2bd9f3ebd5b1866d48e
https://teratail.com/questions/242515

開発環境では全て問題なく動いており、本番環境での問題と考えています。
また、unicorn及びnginxにエラーは出ておりません。
railsのエラー画面が出ていることから、unicornとnginx間での連携は問題ないと考えています。

私と同じ環境、状況で同様のエラーに遭遇している方が少なく、行き詰まりつつあります。
アドバイスお持ちしております。
どうぞよろしくお願いします。

※同内容の質問をteratailにも投稿しております(削除申請中)。
https://teratail.com/questions/262332

3
  • 「・下記記事を参照、実行」で参照されている"RSpecを実行すると'Webpacker can't find ~'となる時の対処法"の記事に書かれている方法で、『ActionView::Template::Error (Webpacker can't find application.js in /var/www/rails/portgram/public/packs/manifest.json.』というエラーは解決できたのですか?
    – Fumu 7
    2020年5月17日 11:43
  • Fumu様 記事中にbin/webpackというディレクトリがありますが私のec2環境にはそれがありませんでした。また、RAILS_ENV=production bundle exec rails webpacker:compileも行いましたが変化ありませんでした。
    – jimbo5922
    2020年5月17日 20:49
  • ですが、そのコマンド実行時に表示されたメッセージにはconfig.eager_load is set to nil. Please update your config/environments/*.rb files accordingly:(略)とありました。その後に表示された内容は既にファイルに記入済みのものなのにこのメッセージが表示されます。
    – jimbo5922
    2020年5月17日 20:56

1 件の回答 1

1

自己解決致しました。
webpacker.ymlの中でproductionに関する設定のうち、
extract_css: trueとあったところを
extract_css: falseとしたところ表示することができました。

私はsassを使っており、.cssのファイルがないので、こうしておかないと
.cssをデフォルトで探しに行くが見つからないという状況に陥るため、エラーが発生したと考えられます。

以下、参考にした記事です。
https://github.com/rails/webpacker/issues/2071

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。