I have tried looking for this problem on the internet and couldn't find anything related. I am trying to build a small project where the body of my HTML will only have one div with id="content" and I have to append the rest of the elements one-by-one to this div. Now I created an element h1 and appended it to the aforementioned div and then hit the npm run build command in the terminal. As soon as webpack emits a bundle, the div id="content" disappears from the body. I am attaching the screenshot of the issue. This is how the index.html look like before I run webpack: Index.html file index.js stays the same after running webpack: index.js file This is how index.html looks like after I run the command npm run build or npm run watch: index.html after running webpack Here is my webpack configuration:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports ={
    entry: {
        index:{
            import: './src/index.js'
        }
    },
    mode: "development",
    devtool: "inline-source-map",
    plugins : [
        new HtmlWebpackPlugin({
            title: 'Resturant Page',
        })
    ],
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            },
            {
                test: /\.png|jpg|jpeg$/,
                type: 'asset/resource',
            },
            {
                test: /\.svg$/,
                type: 'asset/resource',
            },
            {
                test: /\.woff|wof$/,
                type: 'asset/resource',
            }
        ]
    }
}

推荐答案

try 用以下内容替换HtmlWebpackPlugin行:

  plugins: [
    new HtmlWebpackPlugin({
      template: 'src/index.html',
      inject: false, // prevents adding script tag to html so you may do it manually
    })
  ]

我建议保留索引.html在src文件夹中,Webpack将为您将输出复制到dist.

此外,最好让它自动添加,而不是inject: false.

Javascript相关问答推荐

为什么新的Promises会在不需要的情况下被用来等待?

类型错误:tasks.map不是函数(MERN堆栈)

带对角线分隔符的图像滑动器

如何在react + react路由域名中使用DeliverBrowserRouter?

zoom svg以适应圆

如何才能拥有在jQuery终端中执行命令的链接?

深嵌套的ng-container元素仍然可以在Angular 布局组件中正确渲染内容吗?

Angular:动画不启动

Snowflake JavaScript存储过程返回成功,尽管预期失败

用JavaScript复制C#CRC 32生成器

在open shadow—root中匹配时,使用jQuery删除一个封闭的div类

可更改语言的搜索栏

如何在coCos2d-x中更正此错误

如何在.NET Core中将chtml文件链接到Java脚本文件?

同一类的所有div';S的模式窗口

使用NextJS+MongoDB+Prisma ORM获取无效请求正文,无法发布错误

是否可以将Select()和Sample()与Mongoose结合使用?

在D3条形图中对具有相同X值的多条记录进行分组

有没有办法更改Chart.js 3.x.x中主要刻度的字体?

JavaScript&;Reaction-如何避免在不使用字典/对象的情况下出现地狱?