ruby-on-rails - 香草 Rails 安装上的 Webpacker 错误
问题描述
在新的 Ubuntu 20.04 VM 上安装 Rails 的所有依赖项后,我在基本的 Ruby-on-rails 教程中的第一步反复失败。
通过创建香草控制器scaffold
并尝试访问它后,我在服务器控制台中遇到的错误
[Webpacker] Compilation failed:
error Command "webpack" not found.
重现步骤
从https://www.osboxes.org/ubuntu/下载并激活 Ubuntu 20.04.2 Focal Fossa 的 VirtualBox 映像
按照本指南安装 Rails 6.1.3.2、Ruby 3.0.1 和 Node.js 12 (使用 rbenv)
创建一个新应用
rails new testapp cd testapp rails generate scaffold Product name:string rails db:migrate rails server
转到http://127.0.0.1:3000/ - 按预期工作
转到http://127.0.0.1:3000/products - 产生错误
我尝试过但失败的事情(每个都有一个新的 Ubuntu 映像)
bundle exec rails webpacker:install
npm install webpack-dev-server -g
yarn add webpack
yarn install --check-files
- 在没有 rbenv 的情况下全局安装 Ruby
- 按照 Google 结果中“rails install ubuntu 20”的其他指南安装 Rails
- 安装 Node.js 16 或 14
- 使用 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"
解决方案
只有在来宾操作系统的共享文件夹中创建 Rails 应用程序时才会出现问题。
解决方案
- 在主机操作系统中启用符号链接:(
VBoxManage setextradata "VMNAME" VBoxInternal2/SharedFoldersEnableSymlinksCreate/SHARENAME 1
更改VMNAME
和SHARENAME
) - 检查它是否在来宾操作系统的共享文件夹中工作
应该不会产生错误touch test ln -s test test-symlink
- 重新创建您的 Rails 应用程序或通过捆绑程序重新安装所有依赖项
详细信息
我想使用来宾操作系统 (Ubuntu) 作为服务器,而不是让我的主机操作系统与运行 Rails 所需的所有东西混在一起。但我也想在我的主机操作系统上用我最喜欢的文本编辑器编写代码。
所以我在来宾操作系统的共享文件夹中创建了 Rails,这就是问题所在。
我没有在最初的问题中提到共享文件夹的使用,因为我认为这不是实质性的。唉,这是非常可观的。
我的猜测是没有创建 Webpack 工作所需的符号链接。
默认情况下,Virtualbox 中禁用符号链接,作为防止恶意客户操作系统超越主机操作系统的安全预防措施。
推荐阅读
- excel - 过程定义可以嵌套在 Excel VBA 中吗?
- r - 如何编写一个使用 dplyr 来检查变量是否没有 NA 值的函数?
- loops - 在输入上一次最多批处理 10 个
- reactjs - REACT JS:删除1个元素后将位置元素保留在组件中
- jquery - 将对象转换为函数的参数
- apache-spark - 将大量数据从 SQL Server 传输和转换到 Azure SQL Server 的最佳方法。Azure 数据工厂、HDInsight 等
- c++ - 为什么在 SFINAE 期间需要参数中的指针?
- html - Firefox 边框渲染 css 箭头错误
- python - 工人没有定期从 celery-beat 中挑选任务
- python - 我可以不使用 SQL 删除光标指向的记录吗?