Rails 6.1.0 [webpack-cli] TypeError:无法读取未定义的属性“插件”

Posted

技术标签:

【中文标题】Rails 6.1.0 [webpack-cli] TypeError:无法读取未定义的属性“插件”【英文标题】:Rails 6.1.0 [webpack-cli] TypeError: Cannot read property 'plugins' of undefined 【发布时间】:2021-04-16 06:30:58 【问题描述】:

我的带有 Docker 的 Rails+Webpack 应用程序在 6.0.3.4 和 6.0.3.4 中运行良好

"webpack": "^4.44.1",
"webpack-cli": "^3.3.9"

升级到 6.1.0 后 webpack-cli 在@987654322 期间无法读取 environment.js 中的属性“plugins”(或“config”或“loaders”) @。

...Step 20/22 : RUN bundle exec rake RAILS_ENV=staging assets:precompile
 ---> Running in bca898bb9014
yarn install v1.22.5
warning package.json: No license field
warning No license field
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@1.2.13: The platform "linux" is incompatible with this module.
info "fsevents@1.2.13" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
warning " > tempusdominus-bootstrap-4@5.39.0" has unmet peer dependency "moment@^2.29.0".
warning " > tempusdominus-bootstrap-4@5.39.0" has unmet peer dependency "moment-timezone@^0.5.31".
warning " > tempusdominus-bootstrap-4@5.39.0" has unmet peer dependency "tempusdominus-core@5.19.0".
warning "webpack-dev-server > webpack-dev-middleware@3.7.2" has incorrect peer dependency "webpack@^4.0.0".
[4/4] Building fresh packages...
Done in 4.28s.
I, [2021-01-10T14:31:28.268903 #7]  INFO -- : Writing /var/www/<RoR-app>/public/assets/manifest-a72913f4c604bc8f97dd576fc8777bf029401f6af0e14b6a6c9d3874acfe73e4.js
I, [2021-01-10T14:31:28.269857 #7]  INFO -- : Writing /var/www/<RoR-app>/public/assets/manifest-a72913f4c604bc8f97dd576fc8777bf029401f6af0e14b6a6c9d3874acfe73e4.js.gz
Compiling...
Compilation failed:
warning package.json: No license field
[webpack-cli] Failed to load '/var/www/<RoR-app>/config/webpack/production.js'
[webpack-cli] TypeError: Cannot read property 'plugins' of undefined
    at Object.<anonymous> (/var/www/<RoR-app>/config/webpack/environment.js:7:13)
    at Module._compile (/var/www/<RoR-app>/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (/var/www/<RoR-app>/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
    at Object.<anonymous> (/var/www/<RoR-app>/config/webpack/production.js:3:21)
    at Module._compile (/var/www/<RoR-app>/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)

ERROR: Service 'ruby' failed to build: The command '/bin/sh -c bundle exec rake RAILS_ENV=staging assets:precompile' returned a non-zero code: 1

config/webpack/production.js

process.env.NODE_ENV = process.env.NODE_ENV || 'production'
const environment = require('./environment')
module.exports = environment.toWebpackConfig()

config/webpack/environment.js

const  environment  = require('@rails/webpacker');
    const webpack = require('webpack');
    const expose = require('./loaders/expose');
    const coffee = require('./loaders/coffee');
    const erb = require('./loaders/erb');
    
    environment.plugins.prepend('Provide',
      new webpack.ProvidePlugin(
        $: 'jquery',
        jQuery: 'jquery',
        jquery: 'jquery',
        Popper: ['popper.js', 'default'],
        Rails: ['@rails/ujs'],
        moment: 'moment'
      )
    );
    ...

package.json


  "dependencies": 
    "@rails/ujs": "^6.1.1",
    "@rails/webpacker": "^6.0.0-pre.1",
    "bloodhound-js": "^1.2.3",
    "bootstrap": "^4.5.3",
    "clean-webpack-plugin": "^3.0.0",
    "cocoon": "github:nathanvda/cocoon#c24ba53",
    "core-js": "^3.8.2",
    "css-loader": "^5.0.1",
    "exports-loader": "^1.1.1",
    "expose-loader": "^1.0.3",
    "file-loader": "^6.2.0",
    "font-awesome": "^4.7.0",
    "jbuilder": "^0.0.5",
    "jquery": "^3.5.1",
    "jquery-ui": "^1.12.1",
    "jquery-ui-dist": "^1.12.1",
    "jquery-ujs": "^1.2.2",
    "jstree": "^3.3.11",
    "popper.js": "^1.16.1",
    "sidekiq": "^1.1.1",
    "tempusdominus-bootstrap-4": "^5.39.0",
    "turbolinks": "^5.2.0",
    "webpack": "^5.12.2",
    "webpack-cli": "^4.3.1",
    "webpack-dev-middleware": "^4.0.2",
    "webpack-dev-server": "^3.11.1"
  ,
  "devDependencies": 

【问题讨论】:

【参考方案1】:

toWebpackConfig 在 6.x 中不存在。 (https://github.com/rails/webpacker/issues/2875)

webpacker gem v.6.0 尚未发布 (https://rubygems.org/gems/webpacker)

【讨论】:

以上是关于Rails 6.1.0 [webpack-cli] TypeError:无法读取未定义的属性“插件”的主要内容,如果未能解决你的问题,请参考以下文章

升级到 Rails 6.1.0 后 ActiveStorage::Blob 的未定义方法“service_name”

错误:使用 webpack-cli 4.2.0 找不到模块“webpack-cli/bin/config-yargs”

[webpack-cli] Error: Unknown option ‘-p‘ [webpack-cli] Run ‘webpack --help‘ to see available command

由于webpack-cli版本问题造成的错误

如何修复:错误:找不到模块'webpack-cli/bin/config-yargs'?

我不断收到错误:找不到模块'webpack-CLI/bin/config-yargs'