ESLint无法找到名为"eslint-plugin-@typescript-eslint"的插件。

39
我不确定我正在使用的某个东西是否存在错误,或者我是否在这里设置了一些错误,但是当我运行“eslint src --fix”时,我从eslint中获得了有关“eslint-plugin-@typescript-eslint”的错误。
我已经按照@TypeScript-eslint文档中列出的插件指定了插件,但我遇到了这个奇怪的错误,其中eslint试图将“eslint-plugin-”添加到插件名称的开头(包名称为@typescript-eslint/eslint-plugin)。
我正在使用Gatsby和相应的TypeScript插件错误
$ 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"
    }
  }
}

11个回答

21

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


3
我也遇到了同样的问题。你是在全局使用 eslint 吗? - igauravsehrawat
2
我不会全局使用eslint。 - James Mulholland
3
我正在使用最新版本,但遗憾的是它没有解决问题。 - Jamie Hutber
我遇到了同样的问题。从3.3.0升级到^8.36.0解决了这个问题 - 令人惊讶的是。 - worc

14

3
“根”的概念是什么,以及为什么它可以帮助你阅读 - https://eslint.org/docs/user-guide/configuring/configuration-files#cascading-and-hierarchy - MrDywar
谢谢,@MrDywar!那是非常有用的信息! - patriot10

6

我在一个大型的代码仓库中遇到了这个问题,找到了两个解决方案,都对我们起了作用:

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

如果您使用Yarn工作区,可以使用yarn run来完成此操作:
{
  "scripts": {
    "lint": "yarn run eslint src"
  }
}

5

对于未来可能遇到此问题的读者,我在这里分享我的经验。我的情况是,我正在使用基于node:alpine镜像进行多阶段的Docker构建。多阶段构建旨在创建应用程序中dependenciesdevDependencies(在package.json中)之间的分离。

在创建我的Dockerfile过程中,由于需要进行多次修改,经过数小时后,我在Dockerfile开头添加了以下行:

ENV NODE_ENV production 

这会导致npm忽略devDependencies里的包,进而导致ESLint失败(因为它没被安装)。
我将环境变量声明移动到了最终的构建阶段(发布版本),这也是我最初想要的方式,然后npm安装了所有必备的包,ESLint成功运行。
希望这能为某些人节省一些宝贵的时间。

最终,我看到了这个回复,非常感谢。 - Elvin

1

我在Mac上的解决方案是:

  • 进入全局node_modules cd /usr/local/lib/node_modules/
  • 删除全局eslint rm -rf eslint
  • 再次运行命令 eslint index.js app storybook test --fix --ext .js,.ts,.tsx
  • 获得收益

1

以下问题可能导致此问题:

  • ESLint 过时。
  • 全局和本地同时安装了 ESLint。解决方法
  • 项目文件夹中缺少 .eslintrc 配置文件。解决方法:npx eslint --init
  • 在项目父文件夹外有额外的 node_modules 文件夹。
  • 在项目父文件夹外有额外的 .eslintrc 文件。
  • 错误的包配置。阅读更多

0
最近我在尝试在我的项目中使用ESLint和Prettier时遇到了同样的问题,该项目是在2017年创建的,使用Angular 9。经过大量搜索后,我只需将ESLint升级到8+版本即可解决问题。6+版本也可以工作,但7+版本可能会导致另一个错误。这真的很奇怪。
然后我添加了一个新的脚本,如下所示:
./node_modules/eslint/bin/eslint.js ./ --ext .ts,.tsx --config .eslintrc.js --fix

而且 ESLint 功能良好。你可以在 .eslintrc.js 文件中添加一些配置,让 ESLint 知道哪些文件应该被检查和格式化。


0
另一个可能导致这个问题的原因是没有配置ESLint。
在我的情况下,将一个.eslintrc.json文件添加到根目录(具有适当的配置)可以解决这个问题。

1
他现有的使用.eslintrc.js的解决方案也能行得通吗? - misterhtmlcss
2
我看不出来为什么它不能。 - Babak

0
在我的情况下,其中一个父目录有另一个node_modules文件夹。
将包目录移动到父树中没有其他node_modules文件夹的路径中,可以消除错误。

0

由于我现在遇到了与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文件的内容,如果有人现在遇到这个问题。

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