You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Webpack 5配置无法自动生成dist文件夹及HTML、bundle.js文件求助

Webpack5 + React18 无法生成dist文件夹问题解决

问题概述

使用Webpack(v5)搭配React(v18)开发时,配置Webpack后无法自动生成包含HTML和JS文件的dist文件夹,相关配置文件如下:

Webpack配置文件

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

module.exports = {
  mode: "development",
  devServer: {
    static: "./dist",
    host: 'localhost',
    port: 8563,
    open: true,
  },
  entry: "./src/index.js",
  output: {
    path: path.resolve(__dirname, "../dist"),
    filename: "bundle.js",
    clean: true,
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        loader: "babel-loader",
      },
      {
        test: /\.css$/i,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.(gif|png|jpe?g|svg)$/i,
        use: [
          "file-loader",
          {
            loader: "image-webpack-loader",
            options: {
              bypassOnDebug: true, // webpack@1.x
              disable: true, // webpack@2.x and newer
            },
          },
        ],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      filename: "index.html",
      inject: 'body'
    }),
  ],
  devtool: "inline-source-map",
};

package.json文件

{
  "name": "dashboard",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack serve --mode development --config config/webpack.config.js",
    "test": "jest"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@wojtekmaj/enzyme-adapter-react-17": "^0.6.7",
    "@zarconontol/enzyme-adapter-react-18": "^0.7.3",
    "clean-webpack-plugin": "^4.0.0",
    "css-loader": "^6.7.1",
    "file-loader": "^6.2.0",
    "image-webpack-loader": "^8.1.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "style-loader": "^3.3.1"
  },
  "devDependencies": {
    "@babel/core": "^7.19.3",
    "@babel/preset-env": "^7.19.3",
    "@babel/preset-react": "^7.18.6",
    "@testing-library/react": "^13.4.0",
    "babel-loader": "^8.2.5",
    "chai": "^4.3.6",
    "enzyme": "^3.11.0",
    "enzyme-adapter-react-16": "^1.15.6",
    "enzyme-to-json": "^3.6.2",
    "html-webpack-plugin": "^5.5.0",
    "identity-obj-proxy": "^3.0.0",
    "jest": "^29.1.1",
    "jest-transform-stub": "^2.0.0",
    "webpack": "^5.74.0",
    "webpack-cli": "^4.10.0",
    "webpack-dev-server": "^4.11.1"
  }
}

src/index.js文件

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';

ReactDOM.createRoot(document.getElementById('root')).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

HTML模板文件

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
  </head>
  <body>
    <script src="bundle.js"></script>
  </body>
</html>

问题原因

  1. webpack serve不写入磁盘:开发服务器默认将编译后的文件存放在内存中,不会生成实际的dist文件夹,目的是提升开发速度。
  2. 缺少生产打包命令:当前package.json只有启动开发服务器和测试的命令,没有触发打包生成dist的命令。
  3. HtmlWebpackPlugin未指定模板:当前配置未关联你的HTML模板,生成的HTML里没有React挂载需要的root节点,即使生成dist也无法正常运行应用。
  4. 路径潜在问题:webpack配置中output.path为path.resolve(__dirname, "../dist"),需确认配置文件确实在config目录下,否则路径会出错。

解决方案

1. 添加生产打包命令

在package.json的scripts中添加build命令,用于触发Webpack打包生成dist文件夹:

"scripts": {
  "start": "webpack serve --mode development --config config/webpack.config.js",
  "build": "webpack --mode production --config config/webpack.config.js",
  "test": "jest"
}

执行npm run build即可在项目根目录生成dist文件夹。

2. 配置HtmlWebpackPlugin模板

修改Webpack配置中的HtmlWebpackPlugin,指定你的HTML模板路径(假设模板文件在项目根目录的public文件夹下),同时更新模板添加React挂载节点:

plugins: [
  new HtmlWebpackPlugin({
    filename: "index.html",
    inject: 'body',
    template: "./public/index.html" // 替换为你的模板实际路径
  }),
],

更新HTML模板:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
  </head>
  <body>
    <div id="root"></div> <!-- 新增React挂载节点 -->
  </body>
</html>

注:HtmlWebpackPlugin会自动将bundle.js注入到HTML中,无需手动添加<script>标签。

3. 开发环境可选:写入磁盘(不推荐)

如果开发时非要看到磁盘上的dist文件,可以给devServer添加writeToDisk配置,但会降低开发服务器速度:

devServer: {
  static: "./dist",
  host: 'localhost',
  port: 8563,
  open: true,
  writeToDisk: true // 新增该配置
},

4. 确认输出路径正确性

如果webpack配置文件不在config目录下,需要调整output.path,比如配置文件在项目根目录的话,改为:

output: {
  path: path.resolve(__dirname, "dist"),
  filename: "bundle.js",
  clean: true,
},

内容的提问来源于stack exchange,提问作者Moez Ben Rebah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 19:20:39