通过npm install webpack webpack-cli -g 全局安装了webpack后,用webpack打包一个js文件,执行命令webpack example.js -o app.js,报错内容如下:
assets by status 0 bytes [cached] 1 asset
WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value.
Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/
ERROR in main
Module not found: Error: Can't resolve 'example.js' in 'F:\大三-上\web\vue'
Did you mean './example.js'?
Requests that should resolve in the current directory need to start with './'.
根据报错提示,执行命令webpack ./example.js -o ./app.js,依旧报错:
asset main.js 15 bytes [compared for emit] [minimized] (name: main)
./example.js 67 bytes [built] [code generated]
WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value.
Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/
webpack 5.74.0 compiled with 1 warning in 256 ms
解决这个问题需要两处配置:
- 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.js 1.23 KiB [emitted] (name: main)
./example.js 67 bytes [built] [code generated]
webpack 5.74.0 compiled successfully in 114 ms
然后app.js已经被处理过,与原来的example.js大不相同了。
参考链接:https://blog.csdn.net/qq_34979346/article/details/99840181

826

被折叠的 条评论
为什么被折叠?



