簡體   English   中英

@typescript-eslint/eslint-plugin 已安裝,但在 vs code lint 任務中拋出錯誤並請求不存在的 @typescript-eslint 包

[英]@typescript-eslint/eslint-plugin installed but throw error in vs code lint task and ask for non existing @typescript-eslint package

使用"react-scripts": "^4.0.3"樣板創建項目,為了在與typescript項目的反應中包含eslintrc.js文件,我嘗試了eslint --init並且它創建了一個默認的eslintrc.js和以下是內容。

在全球擁有 eslint v 7.32.0

eslintrc.js

module.exports = {
  env: {
    browser: true,
    es2021: true,
  },
  extends: [
    'eslint:recommended',
    'plugin:react/recommended',
    'plugin:@typescript-eslint/recommended',
  ],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 12,
    sourceType: 'module',
  },
  plugins: ['react', '@typescript-eslint'],
  rules: {},
};

但是 vs 代碼中的 es lint 任務拋出錯誤

(節點:37609)UnhandledPromiseRejectionWarning:TypeError:無法加載在“.eslintrc.js”中聲明的插件“@typescript-eslint”:未定義的類擴展值不是構造函數或為空

包.json

"devDependencies": {
    "@types/classnames": "^2.3.1",
    "@types/react-redux": "^7.1.19",
    "@types/react-router-dom": "^5.3.1",
    "@typescript-eslint/parser": "^4.33.0",
    "eslint": "^7.32.0",
    "eslint-plugin-import": "^2.25.2",
    "typescript": "^4.4.4"
  },
 "dependencies": {
    "@types/jest": "^26.0.15",
    "@types/node": "^12.0.0",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-redux": "^7.2.5",
    "react-router-dom": "^5.3.0",
    "react-scripts": "^4.0.3",
    "redux": "^4.1.1",
}

現在當我運行下面的命令來安裝丟失的包時

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

它在終端中引發錯誤

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: react-app@0.1.0
npm ERR! Found: @typescript-eslint/parser@4.33.0
npm ERR! node_modules/@typescript-eslint/parser
npm ERR!   dev @typescript-eslint/parser@"^4.33.0" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer @typescript-eslint/parser@"^5.0.0" from @typescript-eslint/eslint-plugin@5.0.0
npm ERR! node_modules/@typescript-eslint/eslint-plugin
npm ERR!   dev @typescript-eslint/eslint-plugin@"*" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

npm ERR! A complete log of this run can be found in

因此,根據建議嘗試使用選項

npm i --save-dev @typescript-eslint/eslint-plugin --legacy-peer-deps

它已成功安裝,但版本(5.x)高於@typescript-eslint/parser版本,並且如eslint-plugin 文檔中所述

@typescript-eslint/parser 和 @typescript-eslint/eslint-plugin 使用相同的版本號很重要。

所以重新安裝類似版本的包

> npm install --save-dev @typescript-eslint/eslint-plugin@4.33.0

但在 vs 代碼中它仍然拋出錯誤

UnhandledPromiseRejectionWarning:TypeError:無法加載在“.eslintrc.js”中聲明的插件“@typescript-eslint”:未定義的類擴展值不是構造函數或空值

所以我嘗試安裝

npm install --save-dev @typescript-eslint

但它拋出錯誤

npm ERR! code EINVALIDTAGNAME
npm ERR! Invalid tag name "@typescript-eslint": Tags may not have any characters that encodeURIComponent encodes.

還嘗試通過更新 es lint 最新版本 8.0 但它產生了另一個問題。

我知道這是因為我試圖安裝一個不存在的包,但如何解決這個問題? 我在這里錯過了什么嗎?

  • vscode - v 1.61.1
  • ubuntu - v 20.04

檢查eslint 插件命名約定后,發現我必須按照以下語法安裝缺少的@typescript-eslint

npm install --save-dev @typescript-eslint/eslint-plugin

這解決了問題,但未指定反應版本的新錯誤即將出現,因此在“設置”鍵中添加了該錯誤。

eslintrc.js

// eslint-disable-next-line no-undef
module.exports = {
  env: {
    browser: true,
    es2021: true,
  },
  extends: [
    'eslint:recommended',
    'plugin:react/recommended',
    'plugin:@typescript-eslint/recommended',
  ],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 12,
    sourceType: 'module',
  },
  plugins: ['react', '@typescript-eslint'],
  rules: {
    '@typescript-eslint/no-explicit-any': 'off',
    'react/react-in-jsx-scope': 'off',
    '@typescript-eslint/explicit-module-boundary-types': 'off',
  },
  settings: {
    react: {
      version: 'latest',
    },
  },
};

包.json

"devDependencies": {
    "@types/classnames": "^2.3.1",
    "@types/react-redux": "^7.1.19",
    "@types/react-router-dom": "^5.3.1",
    "@typescript-eslint/eslint-plugin": "^4.33.0",
    "@typescript-eslint/parser": "^4.33.0",
    "eslint": "^7.32.0",
    "eslint-plugin-import": "^2.25.2",
    "redux-devtools": "^3.7.0",
    "redux-devtools-extension": "^2.13.9",
    "typescript": "^4.4.4"
  }

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "noImplicitAny": false,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "downlevelIteration": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": ["src"]
}

暫無
暫無

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

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