【问题标题】:Dealing with "Failed to load plugin 'react' declared in '.eslintrc': Cannot find module 'eslint-plugin-react'" ESLint error处理“无法加载在'.eslintrc'中声明的插件'react':找不到模块'eslint-plugin-react'”ESLint错误
【发布时间】:2019-08-19 13:55:01
【问题描述】:

最近我使用“create-react-app”(React 16.9)更新了我的 React 项目
更新前一切正常,但突然出现以下 ESLint 错误:(在输出选项卡中)

 [Error - 16:42:12] 
Failed to load plugin 'react' declared in 'client\.eslintrc': Cannot find module 'eslint-plugin-react'
Require stack:
- C:\Or\Web\VisualizationTool\VisualizationTool\__placeholder__.js
Referenced from: C:\Or\Web\VisualizationTool\VisualizationTool\client\.eslintrc
Happened while validating C:\Or\Web\VisualizationTool\VisualizationTool\client\src\hoc\Layout\Layout.jsx
This can happen for a couple of reasons:
1. The plugin name is spelled incorrectly in an ESLint configuration file (e.g. .eslintrc).
2. If ESLint is installed globally, then make sure 'eslint-plugin-react' is installed globally as well.
3. If ESLint is installed locally, then 'eslint-plugin-react' isn't installed correctly.

我的 .eslintrc 文件:

{
    "env": {
        "browser": true,
        "commonjs": true,
        "es6": true,
        "node": true
    },
    "extends": [
        "react-app",
        "eslint:recommended",
        "plugin:react/recommended"
    ],
    "parser": "babel-eslint",
    "parserOptions": {
        "ecmaVersion": 2018,
        "ecmaFeatures": {
            "jsx": true
        },
        "sourceType": "module"
    },
    "settings": {
        "react": {
            "pragma": "React",
            "version": "16.8"
        }
    },
    "plugins": [
        "react"
    ],
    "rules": {
        "quotes": [
            "error",
            "single",
            {
                "allowTemplateLiterals": true
            }
        ],
        "semi": "off",
        "default-case": [
            "error",
            {
                "commentPattern": "^no default$"
            }
        ],
        "no-new-wrappers": 0,
        "no-mixed-operators": 0,
        "require-atomic-updates": "off",
        "comma-dangle": "off",
        "no-unused-vars": "off",
        "no-useless-constructor": 0,
        "react/jsx-uses-react": "error",
        "react/jsx-uses-vars": "error",
        "react/no-unescaped-entities": 0,
        "react/display-name": 0,
        "jsx-a11y/href-no-hash": "off",
        "jsx-a11y/anchor-is-valid": "off",
        "no-useless-escape": 0,
        "no-console": 0,
        "no-debugger": 0,
        "no-empty": 0,
        "linebreak-style": 0,
        "import/first": 0,
        "import/imports-first": 0,
        "no-shadow": 0,
        "disable-next-line": 0,
        "no-case-declarations": 0,
    }
}

我在全局和本地都安装了 ESLint 和 eslint-plugin-react,还有什么我在这里遗漏的吗?

【问题讨论】:

  • 我遇到的问题是我使用了npm --lonly=prod,它没有安装任何 eslint 依赖项(duh)。希望这可以帮助一些可怜的灵魂拔掉头发。

标签: reactjs visual-studio-code eslint


【解决方案1】:

我在 VS 代码中遇到了同样的问题。在 VS 代码 ESLint 设置中添加如下设置:

{
  "eslint.workingDirectories": [
    "Dir1",
    "Dir2"
  ],
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    "typescript",
    "typescriptreact",
  ]
}

注意:Dir1 和 Dir2 是两个目录,有各自的 .eslintrc 文件。

【讨论】:

  • 你救了我的命!
【解决方案2】:
npm install eslint-plugin-react@latest --save-dev

谢谢!我有同样的问题,安装它对我有用

【讨论】:

    【解决方案3】:

    我也遇到了类似的错误(我不确定原因是否相同,但这就是我发现问题的方式)。

    所以请确保 Require Stack 的路径(在错误中)没有指向您的项目目录之外。

    所以你有一个项目的根目录(你有 node_modules 文件夹等)。在终端$ cd ..(转到保存您的项目的文件夹),然后执行以下操作:

    $ ls -a
    

    如果您看到名为 .eslintrc.js 的文件,请将其删除 ($ rm .eslintr.js)。然后只需重新加载您的项目,问题(至少在我的情况下)就会消失。

    【讨论】:

      【解决方案4】:

      尝试检查您的 ESLint 插件工作目录,因为您的项目使用 create-react-app 更新有一个新目录。有同样的问题,我通过检查 ESLint 插件设置中的工作目录修复了我的问题。

      【讨论】:

        【解决方案5】:

        找到了。
        1. 仅保留“扩展”部分中的“eslint:recommended”。删除所有其他人。
        2. 删除了“插件”部分。
        3.重启VSCode。
        4. 像魅力一样工作!

        我更新后的 .eslintrc 文件如下所示:

        {
            "extends": "eslint:recommended",
            "env": {
                "browser": true,
                "commonjs": true,
                "node": true,
                "es6": true
            },
            "parser": "babel-eslint",
            "parserOptions": {
                "ecmaVersion": 2018,
                "ecmaFeatures": {
                    "jsx": true
                },
                "sourceType": "module"
            },
            "settings": {
                "react": {
                    "pragma": "React",
                    "version": "16.9"
                }
            },
            "rules": {
                "quotes": [
                    "error",
                    "single",
                    {
                        "allowTemplateLiterals": true
                    }
                ],
                "semi": 0,
                "default-case": [
                    "error",
                    {
                        "commentPattern": "^no default$"
                    }
                ],
                "react/jsx-uses-vars": 0,
                "react/react-in-jsx-scope": 0,
                "no-new-wrappers": 0,
                "no-mixed-operators": 0,
                "require-atomic-updates": "off",
                "comma-dangle": "off",
                "no-unused-vars": "off",
                "no-useless-constructor": 0,
                "react/no-unescaped-entities": 0,
                "react/display-name": 0,
                "jsx-a11y/href-no-hash": "off",
                "jsx-a11y/anchor-is-valid": "off",
                "no-useless-escape": 0,
                "no-console": 0,
                "no-debugger": 0,
                "no-empty": 0,
                "linebreak-style": 0,
                "import/first": 0,
                "import/imports-first": 0,
                "no-shadow": 0,
                "disable-next-line": 0,
                "no-case-declarations": 0,
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-07-08
          • 2019-06-28
          • 2020-02-12
          • 1970-01-01
          • 2021-03-31
          • 2022-01-20
          • 1970-01-01
          • 2018-07-04
          相关资源
          最近更新 更多