生产环境下的Rails Webpacker编译错误

24

环境

Rails 6.0.0
Ruby 2.6.0
Amazon Linux2

什么

在此输入图片描述

当我部署我的rails应用程序时,出现了这个错误。

ActionView::Template::Error (Webpacker can't find application in /home/web/www/eloop-regular/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.

1. 如果您没有使用 webpack -w 或 webpack-dev-server,那么您需要将 webpacker.yml 的 compile 值设置为 true。

我的 config/webpacker.yml 文件包含如下内容:

production:
  <<: *default

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

  # Extract and emit a css file
  extract_css: true

  # Cache manifest.json for performance
  cache_manifest: true

2. Webpack尚未重新运行以反映更新。

我运行

$ RAILS_ENV=production bundle exec rails webpacker:compile

3. 您已经错误配置了Webpacker的config/webpacker.yml文件。

在开发环境中,Webpacker可以正常运行。

4. 您的webpack配置没有创建清单文件。

manifest.json文件未被创建。

5个回答

28

我运行预先构建的Rails 6应用程序时遇到了相同的问题。发现在yarn.lock文件中Webpack版本不一致是原因。

然后我运行以下命令:

yarn add @rails/webpacker

bundle update webpacker

这个评论中提到的方法解决了这个问题。


2
yarn add @rails/webpacker 帮助我解决了这个问题。谢谢。 - NBash

3

移除Webpacker:

rm -rf bin/webpack*

安装Webpacker:

rails webpacker:install

production 模式由 Webpacker 编译:

RAILS_ENV=production rails webpacker:compile

关于此问题


在Heroku上运行webpacker:compile命令时,应该使用Heroku bash命令吗? - Obromios
@Obromios 可以试试那里。 - shilovk
那个方法没有起作用。最终,我按照这个答案的建议从application.html中删除了我的stylesheet_pack_tag,问题得以解决:https://dev59.com/k1IH5IYBdhLWcg3wjfm4#59162932。我可以这样做是因为我没有使用webpacker来编译应用程序的CSS文件。 - Obromios

2

我曾经遇到同样的问题。只需要在开发环境下运行:

rails webpacker:install

如果您使用Docker,请运行:

docker exec rails-app-name rails webpacker:install

0
如果你的文件系统中不存在 manifest.json,请使用 rake assets:precompile 命令创建它。

0

对我来说有效的方法是将 <%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> 替换为 <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>application.html.erb文件中


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接