Webpack Error——TypeError: Cannot read property 'properties' of undefined原因分析及解决方法

一、背景

使用webpack构建项目时报错:

                                describe: optionsSchema.definitions.output.properties.path.description,
                                                                           ^
TypeError: Cannot read property 'properties' of undefined
    at module.exports (D:\00_git_pull\Mindsphere-Ali\Starter-Service\FleetManager-Lynn\fleetmanager\node_modules\webpack-cli\bin\config-yargs.js:89:48)
    at Object.<anonymous> (D:\00_git_pull\Mindsphere-Ali\Starter-Service\FleetManager-Lynn\fleetmanager\node_modules\webpack-dev-server\bin\webpack-dev-server.js:84:40)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:279:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:696:3)

二、 原因分析

根据报错,可以发现,报错出现在项目文件夹下的node_modules\webpack-cli\bin\config-yargs.js文件 第89行。

// webpack-cli 2.1.3 config-yargs.js
			"output-path": {
				type: "string",
				describe: optionsSchema.definitions.output.properties.path.description, //报错
				group: OUTPUT_GROUP,
				defaultDescription: "The current directory",
				requiresArg: true
			},

当前webpack-cli的版本为2.1.3,当前webpack版本为4.20.2。查看webpack 的release log,发现
在这里插入图片描述

注意: webpack4.20.0以上版本要使用3.1.1版本的webpack-cli

webpack4.20.0发行日志: https://github.com/webpack/webpack/releases/tag/v4.20.0

三、解决办法

更新webpack-cli到3.1.1及以上版本,执行npm i webpack-cli

+ webpack-cli@3.1.2
updated 1 package and audited 22855 packages in 18.32s

安装最新版3.1.2版本webpacl-cli后,output-path变为

			"output-path": {
				type: "string",
				describe: getSchemaInfo("output.path", "description"), //更新部分
				group: OUTPUT_GROUP,
				defaultDescription: "The current directory",
				requiresArg: true
			},

再次执行webpack命令打包运行项目,done。

i 「wds」: Project is running at http://localhost:3000/webpack-dev-server/
i 「wds」: webpack output is served from /
i 「wds」: Content not from webpack is served from D:\00_git_pull\Mindsphere-Ali\Starter-Service\FleetManager-Lynn\fleetmanager\resources\
i 「wdm」: Compiled successfully.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章