webpack打包报错:if (!scriptUrl) throw new Error(“Automatic publicPath is not supported in this browser“)

2022-07-27,,,

翻车现场

ERROR in Error: D:\Work\test\webpack-demo\05.打包图片资源\src\index.html:104
/******/ if (!scriptUrl) throw new Error(“Automatic publicPath is not supported in this browser”);
Error: Automatic publicPath is not supported in this browser

问题分析

是因为打包资源后不能解析“./”之类的路径,需要通过publicPath配置。

解决方案

在webpack.config.js的output配置中配置publicPath。如下:

module.exports = {
 	// 入口文件
    entry: "/src/index.js",
    output: {
        // 输出文件名
        filename: "bundle.js",
        // 输出路径,__dirname是当前文件的绝对路径,输出到绝对路径下的dist文件夹下
        path: resolve(__dirname, 'dist'),
        // 给打包后资源引入的路径前缀
        // 打包后路径公式:静态资源最终访问路径 = output.publicPath + 资源loader或插件等配置路径
        publicPath: "./"  // 这里不一定非要是'./',根据公式,自行配置
    },
}

如果对你有帮助,可以👍以示鼓励,谢谢😘
webpack打包demo,git地址:https://github.com/OnionMister/webpack-demo.git

本文地址:https://blog.csdn.net/Kindergarten_Sir/article/details/110235547

《webpack打包报错:if (!scriptUrl) throw new Error(“Automatic publicPath is not supported in this browser“).doc》

下载本文的Word格式文档,以方便收藏与打印。