模块构建失败(来自./node_modules/eslint-loader/dist/cjs.js):TypeError:无法读取未定义的属性(读取'getFormatter')

6

我正在运行一个简单的React / Django应用程序,使用webpack构建时出现以下错误:

ERROR in ./src/index.js
Module build failed (from ./node_modules/eslint-loader/dist/cjs.js):
TypeError: Cannot read properties of undefined (reading 'getFormatter')
    at getFormatter (**[Relative path]**/frontend/node_modules/eslint-loader/dist/getOptions.js:52:20)
    at getOptions (**[Relative path]**/frontend/node_modules/eslint-loader/dist/getOptions.js:30:23)
    at Object.loader (**[Relative path]**/frontend/node_modules/eslint-loader/dist/index.js:17:43)

这是我的 package.json 文件:

{
    "name": "frontend",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "start:dev": "webpack serve --config webpack.config.dev.js --port 3000",
        "clean:build": "rimraf ../static && mkdir ../static",
        "prebuild": "run-p clean:build",
        "build": "webpack --config webpack.config.prod.js",
        "postbuild": "rimraf ../static/index.html"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
        "axios": "^0.24.0",
        "react": "^17.0.2",
        "react-dom": "^17.0.2",
        "react-redux": "^7.2.6",
        "redux": "^4.1.2",
        "redux-thunk": "^2.4.0",
        "reselect": "^4.1.1"
    },
    "devDependencies": {
        "@babel/core": "^7.16.0",
        "@babel/node": "^7.16.0",
        "@babel/preset-env": "^7.16.0",
        "@babel/preset-react": "^7.16.0",
        "babel-eslint": "^10.1.0",
        "babel-loader": "^8.2.3",
        "babel-polyfill": "^6.26.0",
        "css-loader": "^6.5.0",
        "cssnano": "^5.0.9",
        "eslint": "^8.1.0",
        "eslint-loader": "^4.0.2",
        "eslint-plugin-import": "^2.25.2",
        "eslint-plugin-react": "^7.26.1",
        "eslint-plugin-react-hooks": "^4.2.0",
        "html-webpack-plugin": "^5.5.0",
        "mini-css-extract-plugin": "^2.4.3",
        "npm-run-all": "^4.1.5",
        "postcss-loader": "^6.2.0",
        "redux-immutable-state-invariant": "^2.1.0",
        "rimraf": "^3.0.2",
        "style-loader": "^3.3.1",
        "webpack": "^5.61.0",
        "webpack-cli": "^4.9.1",
        "webpack-dev-server": "^4.4.0"
    }
}

我的webpack.config.dev.js文件

const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
process.env.NODE_ENV = 'development';
module.exports = {
    mode: 'development',
    target: 'web',
    devtool: 'cheap-module-source-map',
    entry: ['babel-polyfill', './src/index'],
    output: {
        path: path.resolve(__dirname),
        publicPath: '/',
        filename: 'bundle.js'
    },
    devServer: {
        // stats: 'minimal',
        client: {
            overlay: true
        },
        historyApiFallback: true,
        // disableHostCheck: true,
        headers: { 'Access-Control-Allow-Origin': '*' },
        https: false
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env.API_URL': JSON.stringify('http://localhost:8000/api/')
        }),
        new HtmlWebpackPlugin({
            template: './src/index.html',
            favicon: './src/favicon.ico'
        })
    ],
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: 'babel-loader'
                    },
                    'eslint-loader'
                ]
            },
            {
                test: /(\.css)$/,
                use: ['style-loader', 'css-loader']
            }
        ]
    }
};

我不确定是否需要更改配置文件,还是安装的软件包存在问题。若webpack的内部运行方式不太熟悉,请给予指导。

版本信息:

  • node: v17.0.1
  • npm: 8.1.2
  • python: 3.9.6(相信这是一个js/webpack问题)
  • Django: 3.2.8(^^^)
5个回答

14

从eslint-loader迁移到eslint-webpack-plugin https://laurieontech.com/posts/eslint-webpack-plugin/ - Valentin Podkamennyi

0
    "dependencies": {
    "axios": "^0.24.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-redux": "^7.2.6",
    "react-router-dom": "^6.0.1",
    "redux": "^4.1.2",
    "redux-thunk": "^2.4.0",
    "reselect": "^4.1.1"
},
"devDependencies": {
    "@babel/core": "^7.15.0",
    "@babel/node": "^7.14.9",
    "@babel/preset-env": "^7.15.0",
    "@babel/preset-react": "^7.14.5",
    "babel-eslint": "^10.1.0",
    "babel-loader": "^8.2.2",
    "babel-polyfill": "^6.26.0",
    "css-loader": "^6.2.0",
    "cssnano": "^5.0.7",
    "eslint": "^7.32.0",
    "eslint-loader": "^4.0.2",
    "eslint-plugin-import": "^2.24.0",
    "eslint-plugin-react": "^7.24.0",
    "eslint-plugin-react-hooks": "^4.2.0",
    "html-webpack-plugin": "^5.3.2",
    "mini-css-extract-plugin": "^2.2.0",
    "npm-run-all": "^4.1.5",
    "postcss-loader": "^6.1.1",
    "redux-immutable-state-invariant": "^2.1.0",
    "rimraf": "^3.0.2",
    "style-loader": "^3.2.1",
    "webpack": "^5.50.0",
    "webpack-cli": "^4.8.0",
    "webpack-dev-server": "^3.11.2"
}

我回到了以前工作的这些版本,并且现在它正常工作了。我相信这与 eslinteslint-loader 有关。


0

eslint-loader现已被弃用,我改用eslint-webpack-plugin,现在真的很有效!我非常感激,这个问题一直困扰着我! 它确实解决了我的问题,感谢最佳答案,但由于我的声望较低,我无法直接对该答案评论。 此外,这是一个插件而不是loader,所以您应该将其添加到plugins中,不要忘记“new”。在此输入链接描述


目前你的回答不够清晰,请[编辑]以添加更多细节,帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何编写好答案的更多信息。 - Community

0

这是我的代码库链接:https://github.com/ZackKuptsow/olympics_fall @GS_ZS - Zack Kuptsow

0
发现我的 eslint 和 eslint-loader 版本不兼容。
这些版本适用于我: "eslint": "^7.32.0", "eslint-loader": "^4.0.2"。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接