简体   繁体   English

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

[英]ESLint couldn't find the plugin "eslint-plugin-@typescript-eslint"

I'm not sure if there's a bug with something I'm using or whether I've just set something up wrong here, but I'm getting this error from eslint when running eslint src --fix about "eslint-plugin-@typescript-eslint"我不确定我正在使用的东西是否存在错误,或者我是否只是在这里设置了错误,但是在运行eslint src --fix about "eslint-plugin-@ 时,我从 eslint 收到了这个错误打字稿-eslint"

I've specified the plugin as listed in the @TypeScript-eslint docs but I'm getting this weird error where eslint is trying to add 'eslint-plugin-' to the start of the plugin name (the package name is @typescript-eslint/eslint-plugin )我已经指定了@TypeScript-eslint 文档中列出的插件,但是我收到了这个奇怪的错误,其中 eslint 试图将 'eslint-plugin-' 添加到插件名称的开头(package 名称是@typescript-eslint/eslint-plugin typescript- @typescript-eslint/eslint-plugin

I'm using Gatsby and the accompanying TypeScript plugin .我正在使用 Gatsby 和随附的TypeScript 插件

Error错误

$ 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: .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 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"
    }
  }
}

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

my solution on Mac was like我在 Mac 上的解决方案就像

  • go to global node_modules cd /usr/local/lib/node_modules/转到全局 node_modules cd /usr/local/lib/node_modules/
  • delete global eslint rm -rf eslint删除全局 eslint rm -rf eslint
  • run the command once again eslint index.js app storybook test --fix --ext .js,.ts,.tsx再次运行命令eslint index.js app storybook test --fix --ext .js,.ts,.tsx
  • profit利润

I just faced this issue on a large monorepo, found two solutions that fixed it for us:我刚刚在一个大型 monorepo 上遇到了这个问题,找到了两个解决方案为我们解决了这个问题:

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

If you use yarn workspace, yarn run could also do the trick:如果您使用纱线工作区, yarn run也可以做到这一点:

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

Not sure if it's still not resolved.不确定它是否仍然没有解决。 But adding '"root": true' to my .eslintrc.json helped me.但是在我的 .eslintrc.json 中添加 '"root": true' 对我有帮助。

Another reason that could cause this issue is not having ESLint configured.另一个可能导致此问题的原因是没有配置ESLint

In my case, adding a .eslintrc.json file to the root directory (with proper configuration) fixed the issue.就我而言,将.eslintrc.json文件添加到根目录(使用适当的配置)修复了该问题。

Since i faced same issue with eslint now, i'm posting here about the root cause of the issue in my case.由于我现在在使用eslint遇到了同样的问题, eslint我在这里发布了关于我的案例中问题的根本原因。

I installed this eslint-nullish-coalescing plugin which is a fork of eslint to nullish coalescing.我安装了这个eslint-nullish-coalescing插件,它是eslint一个分支来取消合并。 This plugin changed the content of node_module\\.bin\\eslint.cmd file.这个插件改变了node_module\\.bin\\eslint.cmd文件的内容。

from

@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" %*
)

to

@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" %*
)

Because of this, eslint could not find the right plugins for its work, hence the error 😰.因此,eslint 无法为其工作找到合适的插件,因此出现错误😰。

Though OP is not using eslint-nullish-coalescing package, but i would suggest to verify the content of the node_modules\\.bin\\eslint.cmd file if anyone who are facing this issue now.虽然 OP 没有使用eslint-nullish-coalescing包,但如果有人现在面临这个问题,我建议验证node_modules\\.bin\\eslint.cmd文件的内容。

For any future readers who might be facing this issue, in my case I was working with a multi-stage Docker build based on the node:alpine image.对于未来可能面临此问题的任何读者,就我而言,我正在使用基于node:alpine映像的多阶段 Docker 构建。 The multi-stage build was meant to create a separation between the app's dependencies and the devDependencies (in package.json ).多阶段构建旨在分离应用程序的dependenciesdevDependencies (在package.json )。

At some point in creating my Dockerfile , which underwent quite a few modifications over several hours, I added the following line towards the beginning of my Dockerfile :在创建我的Dockerfile某个时候,我在几个小时内进行了相当多的修改,我在我的Dockerfile开头添加了以下行:

ENV NODE_ENV production 

This causes npm to ignore the devDependencies packages, which in turn causes ESLint to fail (because it isn't installed).这会导致npm忽略devDependencies包,进而导致 ESLint 失败(因为它没有安装)。

I moved the environment variable declaration to my final (release) build stage, where I originally wanted it, and then npm installed all required packages and ESLint ran successfully.我将环境变量声明移到我最初想要的最终(发布)构建阶段,然后npm安装了所有必需的包并且 ESLint 成功运行。

Hopefully this saves someone some precious time.希望这可以为某人节省一些宝贵的时间。

In my case one of the parent directories had another node_modules folder.在我的情况下,父目录之一有另一个 node_modules 文件夹。

Moving the package directory into a path with no other node_modules folder in the parent tree removed the error.将包目录移动到父树中没有其他 node_modules 文件夹的路径中消除了错误。

Issues can cause this:问题可能会导致这种情况:

  • Outdated ESLint.过时的 ESLint。
  • ESLint installed globally and locally at the same time. ESLint 同时安装在全局和本地。 Solution .解决方案
  • Missing .eslintrc config file from the project folder.项目文件夹中缺少.eslintrc配置文件。 Solution: npx eslint --init解决方案: npx eslint --init
  • Additional node_modules folder in the project-parent folder outside your project.项目外的 project-parent 文件夹中的其他node_modules文件夹。
  • Additional .eslintrc in the project-parent folder outside your project.项目外的 project-parent 文件夹中的其他.eslintrc
  • Wrong package config.错误的包配置。 Read more .阅读更多

Recently I meet the same problem when I am trying to use eslint&prettier in my project which is created in 2017 and using Angular9.最近,当我尝试在 2017 年创建并使用 Angular9 的项目中使用 eslint&prettier 时,我遇到了同样的问题。 After googled a lot, I just solved it by upgrading eslint to version 8+.在google了很多之后,我只是通过将eslint升级到8+版本来解决它。 Version 6+ also works but version 7+ may cause another error.版本 6+ 也可以,但版本 7+ 可能会导致另一个错误。 That's really weird.这真的很奇怪。

Then I added a new script like => "./node_modules/eslint/bin/eslint.js./ --ext.ts,.tsx --config.eslintrc.js --fix" and eslint just works fine.然后我添加了一个新脚本,例如 => "./node_modules/eslint/bin/eslint.js./ --ext.ts,.tsx --config.eslintrc.js --fix" 并且 eslint 工作正常。 And you can add some configs in.eslintrc.js(on) file to let eslint know which files are supposed to be linted and formatted.你可以在 .eslintrc.js(on) 文件中添加一些配置,让 eslint 知道哪些文件应该被 linted 和格式化。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM