React import error, bable-loader is installed and configured

/ / Days.js

import React, {Component} from "react";
import $ from "jquery";


class Days extends Component {

    constructor(props) {
        super(props);
        this.state = {
            data: [
                {tltle: "ABCDE"}
            ]
        };
    }
    .....

/ / main.js

import React from "react";
import {render} from "react-dom";
import Days from "./Days.js";

........

e:\react06\app\main.js:1
(function (exports, require, module, __filename, __dirname) { import React from "react";
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3

Days.js does not report errors, but main.js does. Beginners, consult.

webpack.config.js

  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: "babel-loader",
          options: {
            "presets": [
              ["env", {
                "targets": {
                  "node": "current"
                }
              }],
              ["react"]
            ]
          }
        }
      }
    ]
  },
....
//package.json
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",

webpack compilation is passed, but the error above is reported when running. I don"t understand!

Apr.11,2021

webpack configuration and document directory send


js has not been converted by babel, because the import syntax is still there, check the js-loader configuration of webpack


directory structure
clipboard.png

webpack.config.js

const path = require('path');

module.exports = {
  entry: "./app/main.js", 

  output: {
    path: path.resolve(__dirname, "dist"), 
    filename: "all.js"
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            "presets": [
              ["env", {
                "targets": {
                  "node": "current"
                }
              }],
              ["react"]
            ]
          }
        }
      }
    ]
  },
  
  node: {
    fs: "empty",
    dgram: "empty"
  }
  
}

"presets": [
              ["env", {
                modules: false
              }],
              ["react"]
            ]
Menu