〇〇 エラーが発生したときの Webpacker::Manifest::MissingEntryError についてのストーリー

Showing /myapp/app/views/layouts/application.html.erb where line #20 raised:

Webpacker can't find application in /myapp/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:
{

application.html.erb的第20行

 <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>

当您尝试在开发环境中打开页面时,会出现上述错误。

环境

我们使用Docker,在生产环境中使用ruby:2.6.6-alpine3.11镜像,在开发环境中使用ruby:2.6.6镜像。(实际上,在开发环境和生产环境中使用不同的镜像不是一个好主意。)

导致这个结果的原因

用于开发环境的Dockerfile


FROM ruby:2.6.6

RUN apt-get update && apt-get install -y  yarn \
...

用于生产环境的Dockerfile


FROM ruby:2.6.6-alpine3.11

RUN apk --no-cache add yarn \
...

关于Yarn的安装,如上所述,根据所使用的映像文件,默认安装了特定版本的Yarn吗?(如果没有特别指定,默认情况下我以为在任何操作系统上都会安装最新版本的Yarn)
在每个容器中检查版本后,发现安装了不同的Yarn,而在开发环境中
错误:没有方案可用;必须至少有一个。而且似乎也没有安装Node.js。

解决方案

使用(操作系统名)和(软件包名)等关键词搜索并查询官方下载方法

本次参考的文章为
在Debian/Ubuntu上安装nodejs和yarn
使用包管理器安装Node.js

参考Debian和Ubuntu基于的Linux发行版,以及企业级Linux/Fedora和Snap软件包部分。因为有Node.js官方的二进制发行版链接,所以可以跳转。

Node.js v14.x:

# Using Debian, as root
curl -fsSL https://deb.nodesource.com/setup_12.x | bash -
apt-get install -y nodejs

将此部分添加到Dockerfile中。

最终的Dockerfile看起来像这样↓

RUN curl -fsSL https://deb.nodesource.com/setup_14.x | bash - 
RUN apt-get update && apt-get install -y  nodejs 
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y  yarn

然而错误仍未得到解决

重新构建了

Webpacker::Manifest::MissingEntryError

出现了上述的错误。

# yarn -v    
1.22.5
# node -v 
v12.22.1 

检查版本后,看起来nodejs和yarn已经正确安装。

解决方案

貌似还没有安装webpacker。执行rails webpacker:install命令即可解决。

随便说说

只需要一个选项:
只有在开发环境中才需要运行此命令`rails webpacker:install`,在测试和生产环境中不安装webpacker也不会报错…。如果有人知道原因,请告诉我,谢谢。