Webpack React 错误: 模块解析失败: 意外的令牌

4

Webpack已安装到React中。但是,当我尝试创建一个dist文件时,出现了错误。如何修复?

enter image description here

从package.json文件中:

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "builder": "webpack"
  },

Webpack.config.js:

const path = require('path');

module.exports = {
  mode: 'development',
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
  },
};

index.js:

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

ReactDOM.render(<App />,
  document.getElementById('root')
);
1个回答

2
您可能需要一个适当的加载器来处理此文件类型。 尝试使用以下方法:
const path = require('path');

module.exports = {
  mode: 'development',
  entry: './src/index.js',
  module: {
    rules: [
      {
        test: /\.(js)$/,
        exclude: /node_modules/,
        use: ['babel-loader']
      }
    ]
  },
  resolve: {
    extensions: ['*', '.js']
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
  },
};


npm i @babel/core @babel/preset-env babel-loader --save-dev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接