簡體   English   中英

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

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

我不確定我正在使用的東西是否存在錯誤,或者我是否只是在這里設置了錯誤,但是在運行eslint src --fix about "eslint-plugin-@ 時,我從 eslint 收到了這個錯誤打字稿-eslint"

我已經指定了@TypeScript-eslint 文檔中列出的插件,但是我收到了這個奇怪的錯誤,其中 eslint 試圖將 'eslint-plugin-' 添加到插件名稱的開頭(package 名稱是@typescript-eslint/eslint-plugin typescript- @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"
    }
  }
}

解決辦法就是升級到最新版本的eslint

我在 Mac 上的解決方案就像

  • 轉到全局 node_modules cd /usr/local/lib/node_modules/
  • 刪除全局 eslint rm -rf eslint
  • 再次運行命令eslint index.js app storybook test --fix --ext .js,.ts,.tsx
  • 利潤

我剛剛在一個大型 monorepo 上遇到了這個問題,找到了兩個解決方案為我們解決了這個問題:

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

如果您使用紗線工作區, yarn run也可以做到這一點:

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

不確定它是否仍然沒有解決。 但是在我的 .eslintrc.json 中添加 '"root": true' 對我有幫助。

另一個可能導致此問題的原因是沒有配置ESLint

就我而言,將.eslintrc.json文件添加到根目錄(使用適當的配置)修復了該問題。

由於我現在在使用eslint遇到了同樣的問題, 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文件的內容。

對於未來可能面臨此問題的任何讀者,就我而言,我正在使用基於node:alpine映像的多階段 Docker 構建。 多階段構建旨在分離應用程序的dependenciesdevDependencies (在package.json )。

在創建我的Dockerfile某個時候,我在幾個小時內進行了相當多的修改,我在我的Dockerfile開頭添加了以下行:

ENV NODE_ENV production 

這會導致npm忽略devDependencies包,進而導致 ESLint 失敗(因為它沒有安裝)。

我將環境變量聲明移到我最初想要的最終(發布)構建階段,然后npm安裝了所有必需的包並且 ESLint 成功運行。

希望這可以為某人節省一些寶貴的時間。

在我的情況下,父目錄之一有另一個 node_modules 文件夾。

將包目錄移動到父樹中沒有其他 node_modules 文件夾的路徑中消除了錯誤。

問題可能會導致這種情況:

  • 過時的 ESLint。
  • ESLint 同時安裝在全局和本地。 解決方案
  • 項目文件夾中缺少.eslintrc配置文件。 解決方案: npx eslint --init
  • 項目外的 project-parent 文件夾中的其他node_modules文件夾。
  • 項目外的 project-parent 文件夾中的其他.eslintrc
  • 錯誤的包配置。 閱讀更多

最近,當我嘗試在 2017 年創建並使用 Angular9 的項目中使用 eslint&prettier 時,我遇到了同樣的問題。 在google了很多之后,我只是通過將eslint升級到8+版本來解決它。 版本 6+ 也可以,但版本 7+ 可能會導致另一個錯誤。 這真的很奇怪。

然后我添加了一個新腳本,例如 => "./node_modules/eslint/bin/eslint.js./ --ext.ts,.tsx --config.eslintrc.js --fix" 並且 eslint 工作正常。 你可以在 .eslintrc.js(on) 文件中添加一些配置,讓 eslint 知道哪些文件應該被 linted 和格式化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM