Babel.js Rails New 6.1应用程序无法处理除默认屏幕之外的任何请求:Webpacker::Manifest::MissingEntryError

3qpi33ja  于 2023-09-28  发布在  Babel
关注(0)|答案(2)|浏览(150)

在一个全新的rails 6.1.7.4应用程序上,当我向除了默认的“Rails::WelcomeController#index”屏幕之外的任何页面发出请求时,都会收到以下错误:
Webpacker::Manifest::MissingEntryError in Blogs#Index

复制步骤:

rails new rails_6-1_app
cd rails_6-1_app
rails g scaffold blog title
rails db:migrate
rails s
# Make request to http://localhost:3000/blogs/

环境:

  • Ruby 2.7.4p191
  • Rails 6.1.7.4
  • 节点16.20.2(但也尝试了节点版本18和20)
  • Yarn1.22.19
  • macOS Ventura 13.5

Webpacker:编译错误

我注意到,当我尝试rails webpacker:compile时,我得到了更多关于发生了什么的信息:

Compiling...
Compilation failed:
node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^

Error: Cannot find package '@babel/plugin-proposal-private-methods' imported from /<Path-TO-MY-APP>/rails_6-1_app/babel-virtual-resolve-base.js

建议via this rails issue修改默认生成的babel.config.js文件中的两行:

// babel.config.js 
// replace this line 
// '@babel/plugin-proposal-private-methods',
// with this line
'@babel/plugin-transform-private-methods', 

// also replace this line
// '@babel/plugin-proposal-private-property-in-object',
// with this line
'@babel/plugin-transform-private-property-in-object',

但是,进行该更改并运行rails webpacker:compile会生成一个新的错误:

Error: error:0308010C:digital envelope routines::unsupported
    at new Hash (node:internal/crypto/hash:69:19)
    at Object.createHash (node:crypto:133:10)
    at module.exports (/<Path-To-My-App>/rails_6-1_app/node_modules/webpack/lib/util/createHash.js:135:53)
utugiqy6

utugiqy61#

在此处发布相同的错误:
https://github.com/rails/rails/issues/48372

说明:

这似乎是因为webpacker生成了一个包含plugin-proposal-private-methodsbabel.config.js。但是,它不会向plugin-proposal-private-methods添加依赖项。相反,它依赖于一个依赖于plugin-proposal-private-methods的包。
最近,这个包被重新命名为plugin-transform-private-methodsbabel/babel#15614,可能是发布了babel/babel@389ecb0
所以现在当babel尝试执行时,它读取babel.config.js文件,看到它需要plugin-proposal-private-methods。尝试加载它,但失败了,因为plugin-proposal-private-methods没有安装(因为它不再是依赖项)。

解决方案:

您可以将babel.config.js中的名称从proposal替换为transform

// '@babel/plugin-proposal-private-methods',
'@babel/plugin-transform-private-methods', 

// '@babel/plugin-proposal-private-property-in-object',
'@babel/plugin-transform-private-property-in-object',

// etc.
r1wp621o

r1wp621o2#

此答案可作为解决方案:我只是禁用了webpacker并恢复使用链轮。显然你根本不需要使用webpacker。我使用this SO post as a reference配置了一个新的rails 6.1应用程序来使用链轮。

复制步骤

首先创建应用程序,但不使用webpacker:

rails new rails_6-1_app --skip-webpack-install
cd rails_6-1_app
rails g scaffold blog title
rails db:migrate

Gemfile中,注解掉gem 'webpacker', '~> 5.0',然后添加以下gem:

# Gemfile
# comment out webpacker
# gem 'webpacker', '~> 5.0'
gem 'sprockets', '~> 4'
gem 'sprockets-rails', :require => 'sprockets/railtie'

更新app/assets/config/manifest.js,使其看起来像这样:

//= link_tree ../images
//= link application.js
//= link application.css

添加文件app/assets/javascripts/application.js如下:

//= require rails-ujs
//= require turbolinks
//= require_tree .

app/views/layouts/application.html.erb中,将javascript_pack_tag替换为javascript_include_tag
运行bundle install
现在,当我用rails s Boot 服务器并通过http://localhost:3000/blogs向应用程序发出请求时,它可以正常工作。

请注意,这只是在开发模式下测试,而不是在生产模式下测试,我没有使用任何资产(图像,附加css,bootstrap,自定义JavaScript等)进行测试。

相关问题