记Vite打包时出现的报错解决:<script src=“xxx.js“> in “/index.html“ can‘t be bundled without type=“module“ attrib

本篇博客记录解决Vite打包时报错:

in “/index.html” can’t be bundled without type=”module” attribute

xxx.css didn’t resolve at build time, it will remain unchangel remain unchanged to be resolved at runtime

当我们通过标签 引入js脚本代码时,出现:can’t be bundled without type=”module” attribute,报错大致意思是我们引入js文件时缺少了type=’module’属性。

 关于标签的属性type=‘module’,如果有疑惑,请看文章:在浏览器中使用javascript module(译) – 简书 (jianshu.com)

 如果你确实使用了JS的模块化,那么你直接加上type=’module’属性基本就能够引入成功,解决报错。

但是,我并没有使用JS模块化,我就想直接引入,全局生效。

其实,问题出现在vite打包时文件访问路径的地方,也就是说,我们通过声明或标签引入静态资源时,引入的路径出现了问题,可能出现npm run dev可以正常运行,而npm run build时就会报错。

=== 解决 ===

我们可以看vite官网的描述:

记Vite打包时出现的报错解决:<script src=“xxx.js“> in “/index.html“ can‘t be bundled without type=“module“ attrib

 记Vite打包时出现的报错解决:<script src=“xxx.js“> in “/index.html“ can‘t be bundled without type=“module“ attrib

 ** 我们可以将静态资源放到根路径下的public目录中,然后通过/开头来引用public中的资源。

 例如官网示例: 

记Vite打包时出现的报错解决:<script src=“xxx.js“> in “/index.html“ can‘t be bundled without type=“module“ attrib

通过/访问public中的静态资源,报错解决,打包正常。 

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/574c60cd5e.html