记一次vuepress报错:UnhandledPromiseRejectionWarning: TypeError: res.getHeader is not a function
在使用vuepress时,按照官网的操作:
在执行完vuepress之后,控制台出现:
VuePress dev server listening at http://localhost:8080/
当将 http://localhost:8080/ 放入浏览器并确定时,会出现以下报错:
(node:8960) UnhandledPromiseRejectionWarning: TypeError: res.getHeader is not a function at processRequest (D:\刘威\AmyProject\0326\vuepress-homepage\node_modules\webpack-dev-middleware\lib\middleware.js:82:18) at ready (D:\刘威\AmyProject\0326\vuepress-homepage\node_modules\webpack-dev-middleware\lib\util.js:51:12) at handleRequest (D:\刘威\AmyProject\0326\vuepress-homepage\node_modules\webpack-dev-middleware\lib\util.js:167:5) at Promise (D:\刘威\AmyProject\0326\vuepress-homepage\node_modules\webpack-dev-middleware\lib\middleware.js:44:7) at new Promise (<anonymous>) at middleware (D:\刘威\AmyProject\0326\vuepress-homepage\node_modules\webpack-dev-middleware\lib\middleware.js:43:12) at Promise.all.Promise (D:\刘威\AmyProject\0326\vuepress-homepage\node_modules\koa-webpack\index.js:43:7) at new Promise (<anonymous>) at D:\刘威\AmyProject\0326\vuepress-homepage\node_modules\koa-webpack\index.js:42:5 at dispatch (D:\刘威\AmyProject\0326\vuepress-homepage\node_modules\koa\node_modules\koa-compose\index.js:42:32) at D:\刘威\AmyProject\0326\vuepress-homepage\node_modules\@shellscape\koa-static\legacy\index.js:58:19 at Generator.throw (<anonymous>) at step (D:\刘威\AmyProject\0326\vuepress-homepage\node_modules\@shellscape\koa-static\legacy\index.js:6:221) at _throw (D:\刘威\AmyProject\0326\vuepress-homepage\node_modules\@shellscape\koa-static\legacy\index.js:6:455) (node:8960) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:8960) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
在这个问题中,通过查找vuepress的issuse我知道是: webpack-dev-middleware 这个中间件的原因。
webpack-dev-middleware的作用就是:生成一个与webpack的compiler绑定的中间件,然后在express启动的服务app中调用这个中间件。
个人推测(通过issuse里讨论):因为webpack-dev-middleware的版本过高,造成了不兼容,所以每次启动都会报这个错
解决方法便是手动设置webpack-dev-middleware的版本:
npm install webpack-dev-middleware@3.6.0
设置成3.6.0版本,然后再一次加载全部依赖:
npm install
最后再次启动项目即可