解决webpack打包问题

通过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

解决这个问题需要两处配置:

  1. package.json文件里:
"scripts": {
    "dev": "webpack --mode development", 
	"build": "webpack --mode production"
}
  1. 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值