javascript - 未捕获的类型错误 : Cannot read property 'locals' of undefined

标签 javascript .net webpack bundle asp.net-core-3.1

我制作了 bundle.js 和 bundle.css 以在我的网络应用程序中使用,
我的 package.json 看起来像这样:

  "dependencies": {
    "bootstrap": "^4.5.3",
    "jquery": "^3.5.1",
    "popper.js": "^1.16.1"
  },
  "devDependencies": {
    "@babel/core": "^7.12.10",
    "@babel/plugin-proposal-object-rest-spread": "^7.12.1",
    "@babel/preset-env": "^7.12.11",
    "babel-loader": "^8.2.2",
    "css-loader": "^5.0.1",
    "file-loader": "^6.2.0",
    "mini-css-extract-plugin": "^1.3.3",
    "node-sass": "^5.0.0",
    "sass-loader": "^10.1.0",
    "style-loader": "^2.0.0",
    "url-loader": "^4.1.1",
    "webpack": "^5.11.0",
    "webpack-cli": "^4.2.0"
  },
我的 webpack.config.js:
const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = (env, argv) => {
    return {
        //Define entry point
        entry: ['./src/index.js', './src/css/index.scss'],

        //Define output point
        output: {
            path: path.resolve(__dirname, 'wwwroot/dist'),
            filename: 'bundle.js'
        },

        module: {
            rules: [
                {
                    test: /\.s[c|a]ss$/,
                    include: path.resolve(__dirname, 'src'),
                    use: [
                        'style-loader',
                        MiniCssExtractPlugin.loader,
                        'css-loader',
                        'sass-loader'
                    ]
                },
                {
                    test: /\.js$/,
                    include: path.resolve(__dirname, 'src'),
                    loader: 'babel-loader',
                    options: {
                        presets: ["@babel/preset-env"],
                        plugins: ['@babel/plugin-proposal-object-rest-spread']
                    }
                },
            ]
        },
        plugins: [
            new webpack.ProvidePlugin({
                $: 'jquery',
                jQuery: 'jquery'
            }),
            new MiniCssExtractPlugin({
                filename: 'bundle.css'
            })
        ]

    };
};
我的 src 结构是这样的:
src
index.js:
import 'jquery';
import 'bootstrap';
import './js/login';
import './js/helpers';
索引.scss:
@import 'bootstrap';
@import 'util.scss';
@import 'login.scss';
@import 'site.css';
我在控制台中收到此错误:
Console
bundle.js
其他一切都工作正常,我对 javascript 没有任何问题,但我不知道如何解决这个错误,这让我很烦恼。
PS:
我一般是 web 开发的新手,昨天开始使用 webpack,想法是在我的 html 中只使用 bundle.js 和 bundle.css 而不为它们分别添加 jquery 和 bootstrap 标签

最佳答案

尝试将 MiniCssExtractPlugin 的选项 esModule 设置为 false

use: [
    'style-loader',
    {
        loader: MiniCssExtractPlugin.loader,
        options: {
            esModule: false,
        },
    },
    'css-loader',
    'sass-loader'
]

关于javascript - 未捕获的类型错误 : Cannot read property 'locals' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65359841/

相关文章:

c# - 如何确定我的 dll 的托管位置

less - 如何使用 webpack less 加载器获取源映射?

javascript - jQuery -> 切换功能

.net - 启动 Windows 应用程序时错误代码 0xc0000135(或 -1073741515 退出代码)意味着什么?

c# - Microsoft.Win32.SaveFileDialog 有效文件名问题

javascript - webpackDevMiddleware 不会自动重新加载

reactjs - 弹出构建组件库后create-react-app的配置

javascript - 全局模式中的前导 ./有何作用?

javascript - Typeahead 模糊事件未在 iPhone 上触发

javascript - 这个 JavaScript 模式的名称是什么?