首页 > 解决方案 > 香草 Rails 安装上的 Webpacker 错误

问题描述

在新的 Ubuntu 20.04 VM 上安装 Rails 的所有依赖项后,我在基本的 Ruby-on-rails 教程中的第一步反复失败。

通过创建香草控制器scaffold并尝试访问它后,我在服务器控制台中遇到的错误

[Webpacker] Compilation failed:
error Command "webpack" not found.

重现步骤

  1. 从https://www.osboxes.org/ubuntu/下载并激活 Ubuntu 20.04.2 Focal Fossa 的 VirtualBox 映像

  2. 按照本指南安装 Rails 6.1.3.2、Ruby 3.0.1 和 Node.js 12 (使用 rbenv)

  3. 创建一个新应用

    rails new testapp
    cd testapp
    rails generate scaffold Product name:string
    rails db:migrate
    rails server
    
  4. 转到http://127.0.0.1:3000/ - 按预期工作
    转到http://127.0.0.1:3000/products - 产生错误

我尝试过但失败的事情(每个都有一个新的 Ubuntu 映像)

  1. bundle exec rails webpacker:install
  2. npm install webpack-dev-server -g
  3. yarn add webpack
  4. yarn install --check-files
  5. 在没有 rbenv 的情况下全局安装 Ruby
  6. 按照 Google 结果中“rails install ubuntu 20”的其他指南安装 Rails
  7. 安装 Node.js 16 或 14
  8. 使用 Ubuntu 版本 21 或 18

彻底失败了,我向你们求助,同胞们,寻求帮助


在浏览器中看到详细错误

Webpacker::Manifest::MissingEntryError in Products#index

Showing /media/sf_coding/testapp/app/views/layouts/application.html.erb where line #10 raised:

Webpacker can't find application.js in /media/sf_coding/testapp/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:
{
}

Extracted source (around line #10):   

    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>

此外,在创建新的 rails 应用程序时可能会出现此警告

warning " > webpack-dev-server@3.11.2" has unmet peer dependency "webpack@^4.0.0 || ^5.0.0"

标签: ruby-on-railsubuntu-20.04

解决方案


只有在来宾操作系统的共享文件夹中创建 Rails 应用程序时才会出现问题。

解决方案

  1. 在主机操作系统中启用符号链接:(
    VBoxManage setextradata "VMNAME" VBoxInternal2/SharedFoldersEnableSymlinksCreate/SHARENAME 1更改VMNAMESHARENAME
  2. 检查它是否在来宾操作系统的共享文件夹中工作
    touch test
    ln -s test test-symlink 
    
    应该不会产生错误
  3. 重新创建您的 Rails 应用程序或通过捆绑程序重新安装所有依赖项

详细信息
我想使用来宾操作系统 (Ubuntu) 作为服务器,而不是让我的主机操作系统与运行 Rails 所需的所有东西混在一起。但我也想在我的主机操作系统上用我最喜欢的文本编辑器编写代码。

所以我在来宾操作系统的共享文件夹中创建了 Rails,这就是问题所在。
我没有在最初的问题中提到共享文件夹的使用,因为我认为这不是实质性的。唉,这是非常可观的。

我的猜测是没有创建 Webpack 工作所需的符号链接。
默认情况下,Virtualbox 中禁用符号链接,作为防止恶意客户操作系统超越主机操作系统的安全预防措施。


推荐阅读