简体   繁体   English

(Webpack) Uncaught TypeError: Swal.mixin is not a function

[英](Webpack) Uncaught TypeError: Swal.mixin is not a function

I am using webpack and webpack-dev-server to run my react app.我正在使用webpackwebpack-dev-server来运行我的反应应用程序。 For some reason the require statement for the sweetalert2 package isn't working, and I get a TypeError when the application runs.由于某种原因, sweetalert2 package 的 require 语句不起作用,并且在应用程序运行时出现 TypeError。

Here is the original code, before webpack:这是 webpack 之前的原始代码:

const Swal = require('sweetalert2');
const alertToast = Swal.mixin({
  toast            : false
});

And the "after-webpack" code I can see in the browser when I try to chase the issue:当我尝试解决问题时,我可以在浏览器中看到“after-webpack”代码:

var Swal = __webpack_require__(/*! sweetalert2 */ "./node_modules/sweetalert2/src/sweetalert2.js");
var alertToast = Swal.mixin({
  toast: false
});

Why is webpack having an issue resolving this module?为什么 webpack 在解决此模块时遇到问题?

Here is my webpack config:这是我的 webpack 配置:

const HtmlWebPackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const path = require('path');

// Config directories
const SRC_DIR = path.resolve(__dirname, 'src');
const OUTPUT_DIR = path.resolve(__dirname, 'build');

// Any directories you will be adding code/files into, need to be
// added to this array so webpack will pick them up
const defaultInclude = [SRC_DIR];

module.exports = {
  context: __dirname,
  entry  : `${SRC_DIR}/index.js`,
  output : {
    path      : OUTPUT_DIR,
    publicPath: '/',
    filename  : 'bundle.js',
    pathinfo  : true
  },
  resolve: {
    alias: {
      src: path.join(__dirname, 'src')
    }
  },
  devServer: {
    historyApiFallback: true
  },
  module: {
    rules: [
      {
        test: /\.(s*)css$/,
        use : [
          'style-loader',
          'css-loader',
          'sass-loader'
        ]
      },
      {
        test   : /\.js$/,
        exclude: /node_modules(?!(\/|\\)js-utils)/,
        loader : 'babel-loader'
      },
      {
        test   : /\.jsx$/,
        exclude: /node_modules(?!(\/|\\)js-utils)/,
        loader : 'babel-loader'
      },
      {
        test   : /\.(jpe?g|png|gif|JPG|webp)$/,
        use    : [{ loader: 'file-loader?name=img/[name]__[hash:base64:5].[ext]' }],
        include: defaultInclude
      },
      {
        test   : /\.(eot|svg|ttf|woff|woff2)$/,
        use    : [{ loader: 'file-loader?name=font/[name]__[hash:base64:5].[ext]' }],
        include: defaultInclude
      }
    ]
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: path.resolve(__dirname, 'public/index.html'),
      filename: 'index.html'
    }),
    new CopyWebpackPlugin([
      { from: 'public' }
    ])
  ],
  target: 'node'
};

My.babelrc我的.babelrc

{
   "presets": [
       [ 
           "@babel/preset-env", {
              "modules": false,
              "targets": {
                "browsers": [
                  "last 2 Chrome versions",
                  "last 2 Firefox versions",
                  "last 2 Safari versions",
                  "last 2 iOS versions",
                  "last 1 Android version",
                  "last 1 ChromeAndroid version",
                  "ie 11"
                ]
              }
           } 
        ],
        "@babel/preset-react"
    ],
    "plugins": [ "@babel/plugin-proposal-class-properties" ]
}

And my file structure还有我的文件结构

  • src源代码
  • containers容器
    • Dashboard仪表板
      • scripts脚本
        • script_where_sweetalert2_imported script_where_sweetalert2_imported
  • index.js index.js
  • webpack.config.js webpack.config.js
  • package.json package.json

And I am starting my app by running webpack-dev-server --mode=development我通过运行webpack-dev-server --mode=development来启动我的应用程序

I fixed the issue by changing:我通过更改解决了这个问题:

const Swal = require('sweetalert2') to import Swal from 'sweetalert2'; const Swal = require('sweetalert2') import Swal from 'sweetalert2';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Sweetalert2 Swal.mixin - 是否可以将输入数据提取到 json/php? - Sweetalert2 Swal.mixin - is it possible to extract input data to json / php? 未捕获的类型错误:__webpack_require__(...).context 不是 function - Uncaught TypeError: __webpack_require__(...).context is not a function Uncaught TypeError: Object(...) is not a function 当与 WebPack 4 捆绑时 - Uncaught TypeError: Object(...) is not a function when bundling with WebPack 4 未捕获的类型错误:__webpack_require__.r 不是函数 - Uncaught TypeError: __webpack_require__.r is not a function 使用 webpack 获取“未捕获的类型错误:$(...).tablesorter is not a function” - Getting 'Uncaught TypeError: $(...).tablesorter is not a function' using webpack 未捕获的类型错误:__webpack_require__.e 不是 function - Uncaught TypeError: __webpack_require__.e is not a function vue webpack-simple模板和Uncaught TypeError :(中间值)不是函数 - vue webpack-simple template and Uncaught TypeError: (intermediate value) is not a function 在Webpack项目中获取“未捕获的TypeError:path.parse不是函数” - Getting “Uncaught TypeError: path.parse is not a function” in Webpack project 未捕获的类型错误:_firebase__WEBPACK_IMPORTED_MODULE_0__.app.auth 不是 function - Uncaught TypeError: _firebase__WEBPACK_IMPORTED_MODULE_0__.app.auth is not a function Uncaught TypeError: Object(…) is not a function after update webpack resolve.modules - Uncaught TypeError: Object(…) is not a function after updating webpack resolve.modules
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM