通过npm install webpack webpack-cli -g全局安装了webpack后,用webpack打包一个js文件,执行命令webpack example.js -o app.js,报错内容如下:
assets by status0bytes[cached]1assetWARNINGinconfiguration The'mode'option has not been set,webpack will fallback to'production'forthisvalue.Set'mode'option to'development'or'production'to enable defaultsforeach environment.You can alsosetit to'none'to disable anydefaultbehavior.Learn more:https://webpack.js.org/configuration/mode/ERRORinmain Module not found:Error:Can't resolve 'example.js' in 'F:\大三-上\web\vue' Did you mean'./example.js'?Requests that should resolveinthe current directory need to startwith'./'.根据报错提示,执行命令webpack ./example.js -o ./app.js,依旧报错:
asset main.js15bytes[comparedforemit][minimized](name:main)./example.js67bytes[built][code generated]WARNINGinconfiguration The'mode'option has not been set,webpack will fallback to'production'forthisvalue.Set'mode'option to'development'or'production'to enable defaultsforeach environment.You can alsosetit to'none'to disable anydefaultbehavior.Learn more:https://webpack.js.org/configuration/mode/webpack5.74.0compiledwith1warningin256ms解决这个问题需要两处配置:
- package.json文件里:
"scripts":{"dev":"webpack --mode development","build":"webpack --mode production"}- webpack.config.js文件里(如果没有这个文件,可以在当前目录下自行创建):
module.exports={mode:"development",// 设置mode development为开发环境,production为生产环境};问题解决,运行webpack ./example.js -o ./app.js得到如下结果:
asset main.js1.23KiB[emitted](name:main)./example.js67bytes[built][code generated]webpack5.74.0compiled successfullyin114ms然后app.js已经被处理过,与原来的example.js大不相同了。
参考链接:https://blog.csdn.net/qq_34979346/article/details/99840181