rails 5.1 Webpack-dev服务器启动错误

时间:2018-03-01 10:48:54

标签: webpack ruby-on-rails-5

ruby version - 2.4.2

rails - 5.1

rails new my_app --webpack=react 

当我尝试使用foreman start -f Procfile.dev./bin/webpack-dev-server

启动服务器时

我收到以下错误,但无法找到原因。任何帮助表示赞赏。

    /Users/shani/Wspace/myapp/node_modules/webpack-dev-server/bin/webpack-dev-server.js:405
    throw e;
    ^

TypeError: Cannot read property 'compile' of undefined
    at addCompilerHooks (/Users/shani/Wspace/myapp/node_modules/webpack-dev-server/lib/Server.js:71:16)
    at new Server (/Users/shani/Wspace/myapp/node_modules/webpack-dev-server/lib/Server.js:81:5)
    at startDevServer (/Users/shani/Wspace/myapp/node_modules/webpack-dev-server/bin/webpack-dev-server.js:398:14)
    at processOptions (/Users/shani/Wspace/myapp/node_modules/webpack-dev-server/bin/webpack-dev-server.js:361:5)
    at Object.<anonymous> (/Users/shani/Wspace/myapp/node_modules/webpack-dev-server/bin/webpack-dev-server.js:504:1)
    at Module._compile (module.js:662:30)
    at Object.Module._extensions..js (module.js:673:10)
    at Module.load (module.js:575:32)
    at tryModuleLoad (module.js:515:12)
    at Function.Module._load (module.js:507:3)

4 个答案:

答案 0 :(得分:3)

我在webpack-dev-server v3.1.0上遇到了完全相同的异常。我降级到v2.11.1并且一切都很好。

https://github.com/rails/webpacker/issues/1303#issuecomment-369090507

答案 1 :(得分:1)

我在webpack-dev-server v3.1.0中遇到了同样的错误。降级到v2.11.2为我解决了这个问题。

答案 2 :(得分:1)

转到config / webpack / environment.js 您需要将webpack配置的代码更改为“.prepend”

应该是这样的:

const { environment } = require('@rails/webpacker')

// Bootstrap 3 has a dependency over jQuery:
const webpack = require('webpack')
environment.plugins.**prepend**('Provide',
  new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery'
  })
)

module.exports = environment

答案 3 :(得分:0)

webpack-dev-server 3.x仅与webpack 4.x兼容,因为对等依赖性已经警告您。

降级到v2.11.2(如上所述)。