【问题标题】:ESLint couldn't find the plugin "eslint-plugin-@typescript-eslint"ESLint 找不到插件“eslint-plugin-@typescript-eslint”
【发布时间】:2019-07-30 07:26:11
【问题描述】:

我不确定我正在使用的东西是否存在错误,或者我是否只是在这里设置了错误,但是在运行 eslint src --fix 时,我从 eslint 收到了这个错误,关于“eslint-plugin- @typescript-eslint"

我已经指定了@TypeScript-eslint 文档中列出的插件,但是我收到了这个奇怪的错误,其中 eslint 试图将 'eslint-plugin-' 添加到插件名称的开头(包名称是@typescript-eslint/eslint-plugin)

我正在使用 Gatsby 和随附的 TypeScript plugin

错误

$ eslint src --fix

Oops! Something went wrong! :(

ESLint: 4.19.1.
ESLint couldn't find the plugin "eslint-plugin-@typescript-eslint". This can happen for a couple different reasons:

1. If ESLint is installed globally, then make sure eslint-plugin-@typescript-eslint is also installed globally. A globally-installed ESLint cannot find a locally-installed plugin.

2. If ESLint is installed locally, then it's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

    npm i eslint-plugin-@typescript-eslint@latest --save-dev

.eslintrc.js:

module.exports = {
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 2018,
    sourceType: 'module',
  },
  env: {
    browser: true,
    node: true,
    es6: true,
    'jest/globals': true,
  },
  plugins: ['@typescript-eslint', 'react', 'jest'],
  extends: [
    'standard',
    'plugin:react/recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:jest/recommended',
    'plugin:prettier/recommended',
    // 'eslint-config-prettier', // must be last
    'prettier/@typescript-eslint',
  ],
  rules: {
    'react/prop-types': 0,
    'jsx-quotes': ['error', 'prefer-single'],
    'react/no-unescaped-entities': 0,
  },
  settings: {
    react: {
      version: 'detect',
    },
    linkComponents: [
      // Components used as alternatives to <a> for linking, eg. <Link to={ url } />
      'Hyperlink',
      { name: 'Link', linkAttribute: 'to' },
    ],
  },
}

package.json

{
  "name": "jmulholland.com",
  "description": "My personal website",
  "license": "MIT",
  "scripts": {
    "dev": "gatsby develop",
    "build": "gatsby build",
    "serve": "gatsby serve",
    "lint": "eslint src --fix",
    "prettier": "prettier \"**/*.+(js|jsx|ts|tsx|json|css|md|mdx|graphql)\"",
    "format": "yarn prettier --write",
    "type-check": "tsc --noEmit",
    "validate": "yarn lint && yarn prettier --list-different"
  },
  "dependencies": {
    "gatsby": "^2.1.4",
    "gatsby-plugin-react-helmet": "^3.0.6",
    "gatsby-plugin-styled-components": "^3.0.5",
    "gatsby-plugin-typescript": "^2.0.10",
    "gatsby-plugin-typography": "^2.2.7",
    "gatsby-remark-prismjs": "^3.2.4",
    "gatsby-source-contentful": "^2.0.29",
    "gatsby-transformer-remark": "^2.3.0",
    "prismjs": "^1.15.0",
    "prop-types": "^15.7.2",
    "react": "^16.8.2",
    "react-dom": "^16.8.2",
    "react-helmet": "^5.2.0",
    "react-typography": "^0.16.18",
    "styled-components": "^4.1.3",
    "typography": "^0.16.18"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^1.4.2",
    "@typescript-eslint/parser": "^1.4.2",
    "babel-jest": "^24.1.0",
    "babel-plugin-styled-components": "^1.10.0",
    "babel-preset-gatsby": "^0.1.8",
    "dotenv": "^6.0.0",
    "eslint": "^4.19.1",
    "eslint-config-prettier": "^4.1.0",
    "eslint-config-standard": "^12.0.0",
    "eslint-plugin-import": "^2.16.0",
    "eslint-plugin-jest": "^22.3.0",
    "eslint-plugin-jsx-a11y": "^6.2.1",
    "eslint-plugin-node": "^8.0.1",
    "eslint-plugin-prettier": "^3.0.1",
    "eslint-plugin-promise": "^4.0.1",
    "eslint-plugin-react": "^7.11.1",
    "eslint-plugin-standard": "^4.0.0",
    "faker": "^4.1.0",
    "husky": "^1.3.1",
    "jest": "^24.1.0",
    "lint-staged": "^8.1.5",
    "prettier": "^1.16.4",
    "typescript": "^3.3.3333"
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  }
}

【问题讨论】:

标签: javascript typescript eslint gatsby


【解决方案1】:

不确定是否仍未解决。但是在我的 .eslintrc.json 中添加 '"root": true' 对我有帮助。

【讨论】:

【解决方案2】:

问题可能导致:

  • 过时的 ESLint。
  • ESLint 同时在全局和本地安装。 Solution
  • 项目文件夹中缺少.eslintrc 配置文件。解决方案:npx eslint --init
  • 项目外的项目父文件夹中的附加 node_modules 文件夹。
  • 额外的.eslintrc 位于项目外部的项目父文件夹中。
  • 错误的包配置。 Read more

【讨论】:

    【解决方案3】:

    我刚刚在一个大型 monorepo 上遇到了这个问题,找到了两个解决方案为我们解决了这个问题:

    {
      "scripts": {
        "lint": "eslint src --resolve-plugins-relative-to ."
      }
    }
    

    如果您使用 yarn 工作区,yarn run 也可以解决问题:

    {
      "scripts": {
        "lint": "yarn run eslint src"
      }
    }
    

    【讨论】:

      【解决方案4】:

      在我的例子中,其中一个父目录有另一个 node_modules 文件夹。

      将包目录移动到父树中没有其他 node_modules 文件夹的路径中删除了错误。

      【讨论】:

        【解决方案5】:

        对于任何可能面临此问题的未来读者,就我而言,我正在使用基于 node:alpine 映像的多阶段 Docker 构建。多阶段构建旨在在应用程序的 dependenciesdevDependencies(在 package.json 中)之间创建分离。

        在创建我的Dockerfile(在几个小时内经历了相当多的修改)的某个时刻,我在Dockerfile 的开头添加了以下行:

        ENV NODE_ENV production 
        

        这会导致 npm 忽略 devDependencies 包,进而导致 ESLint 失败(因为它没有安装)。

        我将环境变量声明移到了我最初想要的最终(发布)构建阶段,然后npm 安装了所有必需的包,ESLint 成功运行。

        希望这可以节省一些宝贵的时间。

        【讨论】:

        • 终于看到了这个回复,非常感谢)
        【解决方案6】:

        我在 Mac 上的解决方案是这样的

        • 转到全局节点模块cd /usr/local/lib/node_modules/
        • 删除全局eslint rm -rf eslint
        • 再次运行命令eslint index.js app storybook test --fix --ext .js,.ts,.tsx
        • 利润

        【讨论】:

          【解决方案7】:

          由于我现在面临与 eslint 相同的问题,因此我在此发布关于我的问题的根本原因。

          我安装了这个eslint-nullish-coalescing 插件,它是eslint 的一个分支,以消除合并。这个插件改变了node_module\.bin\eslint.cmd文件的内容。

          来自

          @IF EXIST "%~dp0\node.exe" (
            "%~dp0\node.exe"  "%~dp0\..\eslint\bin\eslint.js" %*
          ) ELSE (
            @SETLOCAL
            @SET PATHEXT=%PATHEXT:;.JS;=;%
            node  "%~dp0\..\eslint\bin\eslint.js" %*
          )
          

          @IF EXIST "%~dp0\node.exe" (
            "%~dp0\node.exe"  "%~dp0\..\eslint-nullish-coalescing\bin\eslint.js" %*
          ) ELSE (
            @SETLOCAL
            @SET PATHEXT=%PATHEXT:;.JS;=;%
            node  "%~dp0\..\eslint-nullish-coalescing\bin\eslint.js" %*
          )
          

          因此,eslint 无法为其工作找到合适的插件,因此出现错误?。

          虽然 OP 没有使用eslint-nullish-coalescing 包,但如果现在有人遇到此问题,我建议验证node_modules\.bin\eslint.cmd 文件的内容。

          【讨论】:

            【解决方案8】:

            可能导致此问题的另一个原因是未配置 ESLint

            在我的例子中,将.eslintrc.json 文件添加到根目录(使用正确的配置)解决了这个问题。

            【讨论】:

            • 他现有的使用.eslintrc.js的解决方案不也可以吗?
            • 我看不出有什么理由不这样做。
            【解决方案9】:

            解决办法就是升级到最新版本的 eslint

            【讨论】:

            • 我正面临着确切的问题。你在哪里使用eslint 全球?
            • 我没有全局使用 eslint
            • 我正在使用最新版本,遗憾的是它没有修复它。
            猜你喜欢
            • 2019-06-28
            • 1970-01-01
            • 2023-03-14
            • 2018-07-04
            • 2021-04-08
            • 2020-07-16
            • 2023-01-17
            • 2016-05-27
            • 2020-04-26
            相关资源
            最近更新 更多