简体   繁体   English

处理“无法加载在‘.eslintrc’中声明的插件‘react’:找不到模块‘eslint-plugin-react’”ESLint 错误

[英]Dealing with "Failed to load plugin 'react' declared in '.eslintrc': Cannot find module 'eslint-plugin-react'" ESLint error

Recently I updated my react project using "create-react-app" (React 16.9)最近我使用“create-react-app”(React 16.9)更新了我的 React 项目
Everything worked just OK before the update, but suddenly I get following ESLint error: (In the output tab)更新前一切正常,但突然出现以下 ESLint 错误:(在 output 选项卡中)

 [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.

My.eslintrc file:我的.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,
    }
}

I have both ESLint and eslint-plugin-react installed both globally and locally, anything else I am missing here?我在全局和本地都安装了 ESLint 和 eslint-plugin-react,还有什么我在这里遗漏的吗?

npm install eslint-plugin-react@latest --save-dev

Thanks, I had the same problem, installing this worked for me谢谢,我遇到了同样的问题,安装这个对我有用

I had the same problem in VS code.我在 VS 代码中遇到了同样的问题。 Add the following settings in VS code ESLint settings:在 VS Code ESLint 设置中添加以下设置:

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

Note: Dir1 and Dir2 are two directories with their respective .eslintrc files.注意:Dir1 和 Dir2 是两个目录,带有各自的 .eslintrc 文件。

Try to check your ESLint plugin Working directories given that you have a new one for your project with the create-react-app update.尝试检查您的 ESLint 插件工作目录,因为您的项目有一个带有 create-react-app 更新的新目录。 Had the same issue and I fixed mine by checking my working directories in the ESLint plugin settings.遇到了同样的问题,我通过在 ESLint 插件设置中检查我的工作目录来修复我的问题。

I also had a similar error (I can't be sure the reason was the same but this is how I found the issue).我也有类似的错误(我不确定原因是否相同,但这就是我发现问题的方式)。

So make sure the Path of the Require Stack (in the error) doesn't point outside your project directory.因此,请确保 Require Stack 的路径(在错误中)没有指向您的项目目录之外。

So you have a root directory of your project (where you have the node_modules folder and such).所以你有你的项目的根目录(你有 node_modules 文件夹等)。 In the terminal $ cd .. (go to the folder that holds your project) and there do this:在终端$ cd .. (转到保存您的项目的文件夹),然后执行以下操作:

$ ls -a

If you see a file called .eslintrc.js, then remove it ( $ rm .eslintr.js ).如果您看到名为 .eslintrc.js 的文件,请将其删除( $ rm .eslintr.js )。 Then just reload your project and the problem (at least in my case) will be gone.然后只需重新加载您的项目,问题(至少在我的情况下)就会消失。

Found it.找到了。
1. Keep only the "eslint:recommended" in the "extends" section. 1. 在“扩展”部分只保留“eslint:recommended”。 Remove all others.删除所有其他人。
2. Removed the "plugins" section. 2. 删除了“插件”部分。
3. Restart the VSCode. 3. 重新启动 VSCode。
4. Works like a charm! 4. 像魅力一样工作!

My updated .eslintrc file looks like this:我更新的 .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,
    }
}

暂无
暂无

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

相关问题 无法加载在“.eslintrc.json”中声明的插件“prettier”:找不到模块“eslint-plugin-prettier” - Failed to load plugin 'prettier' declared in '.eslintrc.json': Cannot find module 'eslint-plugin-prettier' 错误:无法加载在“.eslintrc”中声明的解析器“babel-eslint”:在 create-react-app 中找不到模块“babel-eslint” - Error: Failed to load parser 'babel-eslint' declared in '.eslintrc': Cannot find module 'babel-eslint' in create-react-app eslint-plugin-react没有找到规则 - eslint-plugin-react does not find a rule eslint-plugin-react是否需要eslint的同行? - eslint-plugin-react requires a peer of eslint? 没有eslint插件反应的ESLint + Airbnb - ESLint + Airbnb without eslint-plugin-react ESLint eslint-plugin-react箭头功能 - ESLint eslint-plugin-react arrow functions 无法加载在“package.json » eslint-config-react-app”中声明的插件“flowtype”:找不到模块“eslint/use-at-your-own-risk” - Failed to load plugin 'flowtype' declared in 'package.json » eslint-config-react-app': Cannot find module 'eslint/use-at-your-own-risk' 错误:无法加载插件导入:在部署 create-react-app 时找不到模块“eslint-plugin-import” - Error: Failed to load plugin import: Cannot find module 'eslint-plugin-import' when deploying a create-react-app eslint-plugin-react返回语法错误与渲染 - eslint-plugin-react return syntax error with Render eslint / eslint-plugin-react:找不到.jsx文件 - eslint/eslint-plugin-react:not finding .jsx files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM