Rails 6.0.3.4 [Webpacker] 编译失败

Posted

技术标签:

【中文标题】Rails 6.0.3.4 [Webpacker] 编译失败【英文标题】:Rails 6.0.3.4 [Webpacker] Compilation failed 【发布时间】:2021-05-06 18:14:18 【问题描述】:

升级到 6.0.3 后 webpackassets:precompile 期间无法读取 environment.js 中的属性“插件”

[Webpacker] Compiling...
[Webpacker] Compilation failed:
[webpack-cli] Failed to load '/planned_maintenance/config/webpack/development.js'
[webpack-cli] TypeError: Cannot read property 'plugins' of undefined
    at Object.<anonymous> (/planned_maintenance/config/webpack/environment.js:4:13)
    at Module._compile (/planned_maintenance/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:973:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Module.require (node:internal/modules/cjs/loader:997:19)
    at require (/planned_maintenance/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
    at Object.<anonymous> (/planned_maintenance/config/webpack/development.js:4:21)
    at Module._compile (/planned_maintenance/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)

Completed 500 Internal Server Error in 3397ms (ActiveRecord: 0.0ms | Allocations: 14850)


  
ActionView::Template::Error (Webpacker can't find application in /planned_maintenance/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:


):
     7: 
     8:     <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
     9:     <%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    10:     <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
    11: 
    12:   </head>
    13: 
  
app/views/layouts/application.html.erb:10

config/webpack/environment.js 看起来像这样:

const  environment  = require('@rails/webpacker')
const webpack = require("webpack")

environment.plugins.append("Provide", new webpack.ProvidePlugin(
  $: 'jquery',
  jQuery: 'jquery',
  Popper: ['popper.js', 'default']
))

module.exports = environment

development.js


process.env.NODE_ENV = process.env.NODE_ENV || 'development'

const environment = require('./environment')

module.exports = environment.toWebpackConfig()

我已经修复了加载失败的问题。它归结为package.json 列出了一个错误的版本号

现在的问题是在 rails 控制台上:

[Webpacker] Compiling...
[Webpacker] Compiled all packs in /Users/Lorenzo/code/LorenzoXavier/planned_maintenance/public/packs
Completed 500 Internal Server Error in 2221ms (ActiveRecord: 0.0ms | Allocations: 18151)


  
ActionView::Template::Error (Webpacker can't find application in /Users/Lorenzo/code/LorenzoXavier/planned_maintenance/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:


):
     7: 
     8:     <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
     9:     <%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    10:     <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
    11: 
    12:   </head>
    13: 
  
app/views/layouts/application.html.erb:10

所以我试过了:

    停止 Rails 服务器 正在运行rails assets:clobber 然后运行bin/webpack-dev-server

反馈了一个错误

node:internal/modules/cjs/loader:928
  throw err;
  ^

Error: Cannot find module 'webpack-cli/bin/config-yargs'
Require stack:
- /Users/Lorenzo/code/LorenzoXavier/planned_maintenance/node_modules/webpack-dev-server/bin/webpack-dev-server.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:925:15)
    at Function.Module._load (node:internal/modules/cjs/loader:769:27)
    at Module.require (node:internal/modules/cjs/loader:997:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at Object.<anonymous> (/Users/Lorenzo/code/LorenzoXavier/planned_maintenance/node_modules/webpack-dev-server/bin/webpack-dev-server.js:65:1)
    at Module._compile (node:internal/modules/cjs/loader:1108:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:973:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12) 
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/Lorenzo/code/LorenzoXavier/planned_maintenance/node_modules/webpack-dev-server/bin/webpack-dev-server.js'
  ]

【问题讨论】:

【参考方案1】:

Environments 为 nil,因为您尚未导入 webpacker。 尝试在您的 const webpack 行上方包含此内容

const  environment  = require('@rails/webpacker');

【讨论】:

感谢您的回复。我添加了线路并重新启动了服务器,但我仍然遇到同样的问题 你能用仍然出错的新代码更新你的帖子吗? 你能把上面提到的错误信息放到 development.js 文件中吗? 我已经解决了加载插件失败的问题,谢谢。以下问题是Cannot find module 'webpack-cli/bin/config-yargs 也许这个对新问题有帮助? ***.com/questions/59611597/…

以上是关于Rails 6.0.3.4 [Webpacker] 编译失败的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Rails 中检查 webpacker 版本?

如何使用 @rails/webpacker 加载本地字体?

带有 Rails(webpacker)的 Vue 错误:“无法获取 /”

Rails/Webpacker 未在生产中加载 Vue 样式

Rails 5/6:如何在 webpacker 中包含 JS 函数?

如何在rails中使用webpacker安装tailwindcss [关闭]