54sword / react-starter

React 同构脚手架
54 stars 17 forks source link

看你的提交记录已经升级到webpack4,但是package.json里面的webpack还是3.10.1 #3

Open dxhuii opened 6 years ago

dxhuii commented 6 years ago

然后webpack-dev-middleware@3要求webpack@4,必须是4,所以一直报错

/mnt/e/group/react-starter/node_modules/webpack-dev-middleware/lib/context.js:95
  context.compiler.hooks.invalid.tap('WebpackDevMiddleware', invalid);
                         ^

TypeError: Cannot read property 'invalid' of undefined
    at ctx (/mnt/e/group/react-starter/node_modules/webpack-dev-middleware/lib/context.js:95:26)
    at wdm (/mnt/e/group/react-starter/node_modules/webpack-dev-middleware/index.js:46:19)
    at exports.default (/mnt/e/group/react-starter/src/server/webpack-hot-middleware.js:9:11)
    at Object.<anonymous> (/mnt/e/group/react-starter/src/server/server.js:34:45)
    at Module._compile (module.js:652:30)
    at loader (/mnt/e/group/react-starter/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/mnt/e/group/react-starter/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/mnt/e/group/react-starter/src/server/index.js:11:1)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
54sword commented 6 years ago

之前有尝试升级到webpack 4,然后因为css modules在服务端渲染有bug,所以又退到了webpack 3的版本

dxhuii commented 6 years ago

但是现在直接npm install 会把webpack-dev-middleware这个装到3.x版本,就报错了。你可以重新clone一下,然后测试一下。会不会出现我这个问题。

dxhuii commented 6 years ago

https://github.com/webpack/webpack-dev-middleware/issues/283 这里有提到。

54sword commented 6 years ago

我刚刚clone测试,npm install 安装的是webpack-dev-middleware@2.0.6的版本,程序运行正常。

dxhuii commented 6 years ago

好神奇,我clone装的是3.1.2,我再试一下。

dxhuii commented 6 years ago

还有,你package.json 里面有 webpack-dev-middleware 这个,可能在安装依赖的时候,默认安装最新的了

54sword commented 6 years ago

webpack-dev-middleware 是 webpack-dev-server 依赖包安装的。 https://github.com/54sword/react-starter/blob/master/package-lock.json#L10987

dxhuii commented 6 years ago

现在跑起来了,我之前用yarn和 cnpm 装都会装成3.1.2版本,现在用npm装就可以了,装的是2.x版本的。

dxhuii commented 6 years ago

有没有打算,啥时候升级到webpack4,现在4.x有没有解决css modules渲染的问题。

54sword commented 6 years ago

有调试过几次,但没调成功,应该是一些老组件不兼容引起的。 之后还会继续试一试,有更新会到更新到github。

dxhuii commented 6 years ago

嗯,好的。我也会时不时的关注一下。

lengzhang commented 6 years ago

你好, 我也遇到了同样的问题.

在你的 package-lock.json 里, webpack-dev-middleware2.0.6, webpack-dev-server3.1.0.

我把 package-lock.json 删了再运行 npm install, 重新安装后 webpack-dev-middleware3.4.0, webpack-dev-server3.1.9 就会报错:

/Users/lengzhang/MySpace/react-starter/node_modules/webpack-dev-middleware/lib/context.js:95
  context.compiler.hooks.invalid.tap('WebpackDevMiddleware', invalid);
                         ^

TypeError: Cannot read property 'invalid' of undefined
    at ctx (/Users/lengzhang/MySpace/react-starter/node_modules/webpack-dev-middleware/lib/context.js:95:26)
    at wdm (/Users/lengzhang/MySpace/react-starter/node_modules/webpack-dev-middleware/index.js:44:19)
    at exports.default (/Users/lengzhang/MySpace/react-starter/src/server/webpack-hot-middleware.js:9:11)
    at Object.<anonymous> (/Users/lengzhang/MySpace/react-starter/src/server/server.js:34:45)
    at Module._compile (module.js:652:30)
    at loader (/Users/lengzhang/MySpace/react-starter/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/lengzhang/MySpace/react-starter/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)

npm install 的时候 npm 匹配了 3.x.x 中的最新版本, 所以安装 webpack-dev-server 的版本不是 3.1.0 而是 3.1.9, webpack-dev-middleware 就升到3.4.0.

webpack-dev-server 锁定在 3.1.0 就能避免 webpack-dev-middleware 就升到3.x.x 了.