ESLint找不到插件“ eslint-plugin- @ typescript-eslint”

Jam*_*and 6 javascript typescript eslint gatsby

我不确定我所使用的东西是否存在错误,或者我是否在这里设置了错误,但是运行eslint src --fix“ eslint-plugin- @ typescript-eslint” 时,我从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
Run Code Online (Sandbox Code Playgroud)

.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' },
    ],
  },
}
Run Code Online (Sandbox Code Playgroud)

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"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 14

添加"root": true.eslintrc.json帮助了我。

"root": true通常是一种很好的 ESLint 实践,表明此文件是项目使用的根级别文件,并且 ESLint 不应在该目录之外搜索配置文件。

https://typescript-eslint.io/getting-started#details

  • “根”是什么,以及为什么它可以帮助您阅读 - https://eslint.org/docs/user-guide/configuring/configuration-files#cascading-and-hierarchy (3认同)

Jam*_*and 11

解决方案只是升级到eslint的最新版本

  • 我正在使用最新版本,遗憾的是它没有修复它。 (3认同)
  • 我面临着确切的问题。您在全球范围内使用“eslint”的地方? (2认同)
  • 我不在全球范围内使用 eslint (2认同)

Gom*_*mah 6

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

{
  "scripts": {
    "lint": "eslint src --resolve-plugins-relative-to ."
  }
}
Run Code Online (Sandbox Code Playgroud)

如果您使用纱线工作区,yarn run也可以做到这一点:

{
  "scripts": {
    "lint": "yarn run eslint src"
  }
}
Run Code Online (Sandbox Code Playgroud)


Cha*_*les 5

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

在创建我的 的过程中Dockerfile,在几个小时内经历了相当多的修改,我在我的 的开头添加了以下行Dockerfile

ENV NODE_ENV production 
Run Code Online (Sandbox Code Playgroud)

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

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

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