使用webpack命令打包时,报错TypeError: Cannot read property 'presetToOptions' of undefined的解决办法

Posted 炸呼呼er

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用webpack命令打包时,报错TypeError: Cannot read property 'presetToOptions' of undefined的解决办法相关的知识,希望对你有一定的参考价值。

我只安装了webpack,没有安装webpack-cli,第一次输入webpack打包时,提示

One CLI for webpack must be installed. These are recommended choices, delivered as separate packages:
 - webpack-cli (https://github.com/webpack/webpack-cli)
   The original webpack full-featured CLI.
We will use "npm" to install the CLI via "npm install -D".
Do you want to install 'webpack-cli' (yes/no): 

输入yes后报错

Error: Cannot find module 'webpack-cli'
Require stack:
- C:UsersAdministratorAppDataRoaming
pm
ode_moduleswebpackinwebpack.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:772:15)
    at Function.Module._load (internal/modules/cjs/loader.js:677:27)
    at Module.require (internal/modules/cjs/loader.js:830:19)
    at require (internal/modules/cjs/helpers.js:68:18)
    at C:UsersAdministratorAppDataRoaming
pm
ode_moduleswebpackinwebpack.js:143:5
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\Users\Administrator\AppData\Roaming\npm\node_modules\webpack\bin\webpack.js'
  ]
}

解决办法:输入命令npm install webpack-cli -g全局安装一下webpack-cli
再次运行webpack命令打包,出现新错误

F:mywebalicevuevue-cms
ode_moduleswebpack-cliincli.js:137
                        const statsPresetToOptions = require("webpack").Stats.presetToOptions;
                                                                             ^

TypeError: Cannot read property 'presetToOptions' of undefined
    at processOptions (F:mywebalicevuevue-cms
ode_moduleswebpack-cliincli.js:137:57)
    at F:mywebalicevuevue-cms
ode_moduleswebpack-cliincli.js:364:3
    at Object.parse (F:mywebalicevuevue-cms
ode_moduleswebpack-cli
ode_modulesyargsyargs.js:567:18)
    at F:mywebalicevuevue-cms
ode_moduleswebpack-cliincli.js:49:8
    at Object.<anonymous> (F:mywebalicevuevue-cms
ode_moduleswebpack-cliincli.js:366:3)
    at Module._compile (internal/modules/cjs/loader.js:936:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Module.load (internal/modules/cjs/loader.js:790:32)
    at Function.Module._load (internal/modules/cjs/loader.js:703:12)
    at Module.require (internal/modules/cjs/loader.js:830:19)
    at require (internal/modules/cjs/helpers.js:68:18)
    at module.exports (C:UsersAdministratorAppDataRoaming
pm
ode_moduleswebpack-cli
ode_modulesimport-localindex.js:16:66)
    at C:UsersAdministratorAppDataRoaming
pm
ode_moduleswebpack-cliincli.js:15:6
    at Object.<anonymous> (C:UsersAdministratorAppDataRoaming
pm
ode_moduleswebpack-cliincli.js:366:3)
    at Module._compile (internal/modules/cjs/loader.js:936:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Module.load (internal/modules/cjs/loader.js:790:32)
    at Function.Module._load (internal/modules/cjs/loader.js:703:12)
    at Module.require (internal/modules/cjs/loader.js:830:19)
    at require (internal/modules/cjs/helpers.js:68:18)
    at Object.<anonymous> (C:UsersAdministratorAppDataRoaming
pm
ode_moduleswebpackinwebpack.js:156:2)
    at Module._compile (internal/modules/cjs/loader.js:936:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Module.load (internal/modules/cjs/loader.js:790:32)
    at Function.Module._load (internal/modules/cjs/loader.js:703:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
    at internal/main/run_main_module.js:17:11

解决办法:根据错误提示信息,打开node_moduleswebpack-cliincli.js,第137行,把const statsPresetToOptions = require("webpack").Stats.presetToOptions;中间Stats下标的这个.删掉。
保存后再次运行webpack就成功了!

以上是关于使用webpack命令打包时,报错TypeError: Cannot read property 'presetToOptions' of undefined的解决办法的主要内容,如果未能解决你的问题,请参考以下文章

实现webpack的实时打包构建

Webpack打包时出现require报错问题

webpack打包---报错内存溢出javaScript heap out of memory

react 打包项目npm start 报错 Failed at the start script.

html-webpack-plugin打包报错

webpack打包报错