webpack 模块剖析失败:'import'和'export'只能与'sourceType:模块'(1:0)

voase2hg  于 2022-11-13  发布在  Webpack
关注(0)|答案(1)|浏览(1321)

我需要这个错误的帮助,我没有太多的经验与Babel当我试图运行JS应用程序抛出这个错误。我已经经历了各种解决方案,但我不能解决它。当我删除"./src/index.js"从webpack配置错误消失,但应用程序不工作。有人知道解决方案吗?

ERROR in ./node_modules/@babel/runtime/regenerator/index.js 1:0
Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (1:0)
File was processed with these loaders:
 * ./node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
> import _typeof from "@babel/runtime/helpers/typeof";
| 
| // TODO(Babel 8): Remove this file.
 @ ./src/index.js 3:0-61 19:54-78 21:11-35 66:61-85 67:9-33

ERROR in   Error: Child compilation failed:
  Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (1:0)
  File was processed with these loaders:
   * ./node_modules/babel-loader/lib/index.js
  You may need an additional loader to handle the result of these loaders.
  > import _typeof from "@babel/runtime/helpers/typeof";
  | 
  | // TODO(Babel 8): Remove this file.:
  SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' (1:0)
  ModuleParseError: Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (1:0)
  File was processed with these loaders:
   * ./node_modules/babel-loader/lib/index.js
  You may need an additional loader to handle the result of these loaders.
  > import _typeof from "@babel/runtime/helpers/typeof";
  | 
  | // TODO(Babel 8): Remove this file.
  
  - NormalModule.js:976 handleParseError
    [wolf3d-readyplayerme-threejs-boilerplate]/[webpack]/lib/NormalModule.js:976:19
  
 

ERROR in   Error: Child compilation failed:
  Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (1:0)
  File was processed with these loaders:
   * ./node_modules/babel-loader/lib/index.js
  You may need an additional loader to handle the result of these loaders.
  > import _typeof from "@babel/runtime/helpers/typeof";
  | 
  | // TODO(Babel 8): Remove this file.:
  SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' (1:0)
  ModuleParseError: Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (1:0)
  File was processed with these loaders:
   * ./node_modules/babel-loader/lib/index.js
  You may need an additional loader to handle the result of these loaders.
  > import _typeof from "@babel/runtime/helpers/typeof";
  | 
  | // TODO(Babel 8): Remove this file.
  
  - NormalModule.js:976 handleParseError
    [wolf3d-readyplayerme-threejs-boilerplate]/[webpack]/lib/NormalModule.js:976:19

  - child-compiler.js:131 
    [wolf3d-readyplayerme-threejs-boilerplate]/[html-webpack-plugin]/lib/child-compiler.js:131:18
  
  - Compiler.js:551 finalCallback
    [wolf3d-readyplayerme-threejs-boilerplate]/[webpack]/lib/Compiler.js:551:5
  

1 ERROR in child compilations (Use 'stats.children: true' resp. '--stats-children' for more details)
webpack 5.74.0 compiled with 4 errors and 11 warnings in 16857 ms

下面是我的webpack.config.js文件

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

module.exports = (env) => {
  const isProd = env && env.prod;
  const config = {
    mode: isProd ? "production" : "development",
    performance: { hints: false },
    entry: ["babel-polyfill", "./src/index.js"],
    plugins: [
      new webpack.DefinePlugin({
        DEVELOPMENT: !isProd,
      }),
      new webpack.ProvidePlugin({
        process: 'process/browser',
      }),
      new HtmlWebpackPlugin({
        template: "./src/index.js",
      }),
      new CopyWebpackPlugin({
        patterns: [{ from: "src/assets", to: "assets" }],
      }),
      new HtmlWebpackPlugin({
        title: isProd ? "Production" : "Development",
        meta: {
          viewport:
            "width=device-width, initial-scale=1, shrink-to-fit=no, maximum-scale=1.0, user-scalable=no",
        },
      }),
    ],
    output: {
      filename: "[name].js",
      path: path.resolve(__dirname, "dist"),
    },
    resolve: {
      fallback: {
        util: require.resolve("util/")
      }
    },
    module: {
      rules: [
        {
          test: /\.(glsl|vs|fs|vert|frag)$/,
          use: ["raw-loader", "glslify-loader"],
        },
        {
          test: /\.js$/,
          use: {
            loader: "babel-loader",
            options: {
              compact: false,
              presets: [["@babel/preset-env"]],
              plugins: [
                ['@babel/plugin-transform-runtime', { "regenerator": true } ],
                "@babel/plugin-proposal-optional-chaining",
                "@babel/plugin-syntax-nullish-coalescing-operator",
              ],
            },
          },
        },
      ],
    },
  };

  if (!isProd) {
    config.devtool = "eval";
  }

  return config;
};
enyaitl3

enyaitl31#

typeof x更改为typeof(x)似乎可以修复此问题。
与巴别塔和这条线有关:
import _typeof from "@babel/runtime/helpers/typeof
也许配置文件缺少了什么?
编辑:将sourceType: 'unambiguous',添加到babel-loader的webpack配置文件的选项中可以修复这个问题。如果你在那里管理选项,你可能需要编辑babel.config。

相关问题