找不到规则 'import/extensions' 的 ESLint 定义

ESLint Definition for rule 'import/extensions' was not found

我在 VS Code 中使用 ESLint 的所有 TypeScript 文件都出现以下两个错误:

Definition for rule 'import/extensions' was not found.eslint(import/extensions)
Definition for rule 'import/no-extraneous-dependencies' was not found.eslint(import/no-extraneous-dependencies)

VSC 问题窗格的屏幕截图:

注意可能的重复项

有许多关于不同模块的类似问题,甚至还有一些关于 import/extensions 的问题,但是 none 的建议解决方案有所帮助。我都试过了。在输入此问题时,我还尝试了 Stack Overflow 建议的每个线程中发布的每个解决方案。

这是我的 .eslintrc.json:

{
  "env": {
      "es2021": true,
      "node": true
  },
  "extends": [
      "airbnb-typescript/base",
      "plugin:@typescript-eslint/recommended"
  ],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
      "ecmaVersion": 12,
      "sourceType": "module",
      "project": "./tsconfig.json"
  },
  "plugins": [
      "@typescript-eslint"
  ],
  "rules": {
      "@typescript-eslint/no-use-before-define": "off"
  }
}

package.json:

{
  "name": "graph-userdata-gateway",
  "version": "1.0.0",
  "description": "Gateway for PowerApps Microsoft Graph API custom connector to query user data with application permissions.",
  "main": "src/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git@gitlab.yobitti.fi:powerapps/graph-userdata-gateway.git"
  },
  "author": "Benjamin Pettinen / YO-bitti Oy",
  "license": "UNLICENSED",
  "dependencies": {
    "@microsoft/microsoft-graph-client": "^3.0.0",
    "dedent": "^0.7.0",
    "express": "^4.17.1",
    "isomorphic-fetch": "^3.0.0",
    "md5": "^2.3.0",
    "node-fetch": "^2.6.1"
  },
  "devDependencies": {
    "@types/dedent": "^0.7.0",
    "@types/express": "^4.17.13",
    "@types/isomorphic-fetch": "0.0.35",
    "@types/md5": "^2.3.1",
    "@types/node-fetch": "^2.5.12",
    "@typescript-eslint/eslint-plugin": "^4.29.2",
    "@typescript-eslint/parser": "^4.29.2",
    "eslint": "^7.32.0",
    "eslint-config-airbnb-base": "^14.2.1",
    "eslint-config-airbnb-typescript": "^13.0.0",
    "eslint-plugin-import": "^2.24.1",
    "typescript": "^4.3.5"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "ES6",
    "module": "CommonJS",
    "esModuleInterop": true,
    "noImplicitAny": true,
    "moduleResolution": "Node",
    "outDir": "dist",
    "sourceMap": true
  }
}

我试过删除整个 node_mobules 并重新 运行 npm install 以及 .eslintrc.json 中的 extends。如果我删除 airbnb-typescript/base 错误消失,但我失去了 ESLint 的 Airbnb 风格。使用 airbnb-base 可以,但 ESLint 会抱怨 Missing file extension for abc.tsUnable to resolve path to module abc。我也多次重启了 VSC 和 ESLint 服务器。

你错过了在你的 eslint.json 文件中添加这个。

"plugins": ["import"],

或者,

"extends": ["plugin:import/recommended"]

参考:https://github.com/import-js/eslint-plugin-import#resolvers

对于那些在 Typescript Airbnb 配置升级到 v13+ 后来到这里并想了解发生了什么变化的人:

Read carefully the most recent README of the eslint-config-airbnb-typescript project.

Make sure you have the regular Airbnb config setup. See eslint-config-airbnb, or eslint-config-airbnb-base if you're not using React.

对于您的旧设置来说,听起来像是新东西,而且确实如此。 那是因为有一个 breaking change in v13

让我们听从建议。

根据 eslint-config-airbnb-base instructions,您应该将 eslint-config-airbnb-base 及其对等部门添加到您的 package.json。最简单的方法:npx install-peerdeps --dev eslint-config-airbnb-base。现在,此命令会将 (eslint-config-airbnb-baseeslint-plugin-import) 添加到您的 package.json:

     "eslint": "^8.7.0",
+    "eslint-config-airbnb-base": "^15.0.0",
     "eslint-config-airbnb-typescript": "^16.1.0",
+    "eslint-plugin-import": "^2.25.4",

最后一步是在 airbnb-typescript/baseextends eslint 配置数组之前添加 airbnb-base

   plugins: ["@typescript-eslint"],
   extends: [
+    "airbnb-base",
     "airbnb-typescript/base",

就是这样!问题现在应该已经解决了。

就我而言,我能够通过将“allowSyntheticDefaultImports”设置添加到“tsconfig.json”中的“compilerOptions”来修复它。

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
  }
}