首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模块解析失败:“导入”和“导出”只能与“sourceType:模块”一起出现(1:0)

模块解析失败:“导入”和“导出”只能与“sourceType:模块”一起出现(1:0)
EN

Stack Overflow用户
提问于 2022-11-01 13:07:56
回答 1查看 446关注 0票数 0

我需要帮助处理这个错误,我没有太多的经验,巴贝尔,当我试图运行的JS应用程序,它抛出这个错误。我经历过各种各样的解决办法,但我无法解决。当我从webpack中删除"./src/index.js"时,配置错误就消失了,但是应用程序没有功能。有人知道解决办法吗?

代码语言:javascript
复制
  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文件

代码语言:javascript
复制
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;
};
EN

回答 1

Stack Overflow用户

发布于 2022-11-06 10:36:06

typeof x改为typeof(x)似乎可以解决这个问题。

必须对babel和这句话做些什么:

import _typeof from "@babel/runtime/helpers/typeof

也许配置文件漏掉了什么?

编辑:将sourceType: 'unambiguous',添加到webpack配置文件的选项中,并将其修复.如果您在那里管理选项,您可能必须编辑babel.config。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74276447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档