Javascript 找不到网页包模块功能
我有一个简单的项目,根文件夹中有几个html文件,脚本目录中有几个javascript模块 我想现在就开始使用webpack,因为我想缩小并开始使用环境。在我开始使用webpack之前,一切都正常 我阅读了Webpack5的文档,但找不到任何解决我问题的方法。所以我想出了这个webpack.config.jsJavascript 找不到网页包模块功能,javascript,webpack,ecmascript-6,Javascript,Webpack,Ecmascript 6,我有一个简单的项目,根文件夹中有几个html文件,脚本目录中有几个javascript模块 我想现在就开始使用webpack,因为我想缩小并开始使用环境。在我开始使用webpack之前,一切都正常 我阅读了Webpack5的文档,但找不到任何解决我问题的方法。所以我想出了这个webpack.config.js const path = require('path'); const HtmlWebpackPlugin = require("html-webpack-plugin"
const path = require('path');
const HtmlWebpackPlugin = require("html-webpack-plugin")
module.exports = {
entry: {
lib: "./scripts/lib.js"
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
},
devServer: {
contentBase: "./dist",
inline: true, //Enable watch and live reload
host: "localhost",
port: 8080,
stats: "errors-only",
https: false
},
plugins: [
new HtmlWebpackPlugin({
filename: "index.html",
template: "index.html",
hash: true
})
],
module: {
rules: [
{
test: /\.html$/i,
loader: 'html-loader',
},
],
},
};
My index.html导入并使用如下模块函数:
<script type="module">
import * as lib from "./scripts/lib.js";
window.lib = lib;
</script>
<body onload="lib.myFn()">
</body>
从“/scripts/lib.js”导入*作为库;
window.lib=lib;
但在运行此网页包(使用webpack service
)后,我收到了“不是函数”或“未找到模块”错误
如何使用webpack编译和运行现有项目而不更改目录结构并修复此错误,以便在index.html中开始使用lib.js中的函数?谢谢。在我看来,您不应该从scripts/lib.js导入lib,只是/lib.js可能?您应该从目标位置导入,而不是从源位置导入。(我将我的答案转换为注释,因为我只有一半的答案,如果答案为0,可能其他人更可能看到)