带有 babel 的 webpack 显示错误 Module build failed (from ./node_modules/babel-loader/lib/index.js):

webpack with babel showing error Module build failed (from ./node_modules/babel-loader/lib/index.js):

我正在尝试使用 webpack 和 babel 创建一个 React 应用程序,但是当我实际启动 webpack 开发服务器时,使用 yarn run start,它显示以下错误:-

我正在使用 babel-loader 8.1.0 版和@babel/core 7.10.2 版

我的package.json:-

{
  "name": "reactTemplate",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start": "webpack-dev-server --mode development --open --hot",
    "build": "webpack --mode production"
  },
  "dependencies": {
    "react": "^16.13.1",
    "react-dom": "^16.13.1"
  },
  "devDependencies": {
    "babel-loader": "^8.1.0",
    "@babel/core": "^7.10.2",
    "@babel/preset-env": "^7.10.2",
    "@babel/preset-react": "^7.10.1",
    "html-webpack-plugin": "^4.3.0",
    "webpack": "^4.43.0",
    "webpack-cli": "^3.3.11",
    "webpack-dev-server": "^3.11.0"
  }
}

我的.babelrc:-

{
  "presets": ["@babel/preset-env", "@babel/preset-react"]
}

我的webpack.config.js:-

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

module.exports = {
  entry: './src/index',
  output: {
    path: path.join(__dirname, './dist'),
    filename: 'index_bundle.js',
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        },
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html',
    }),
  ],
};

如果您是 运行 一个非常简单的 hello world 示例并且扩展名是 js,那么您的设置似乎没问题。如果您也使用 jsx 文件,请将其添加到 webpack

  test: /\.(js|jsx)$/,