Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser for gatsby/typescript

Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser for gatsby/typescript

我已经阅读了很多具有相同错误消息的帖子,但所有帖子都与他们的 .eslintrc 或 babel 配置文件有关。我在这里使用他们网站上的 gatsby/typescript 入门工具包:https://www.gatsbyjs.com/starters/jpedroschmitz/gatsby-starter-ts

问题是,当我从根目录创建一个 server/index.ts 文件时,由于它在 src 文件夹之外,在文件的第一行,我得到:解析错误:“parserOptions.project" 已为 @typescript-eslint/parser 设置。该文件与您的项目配置不匹配:server/index.ts。 该文件必须至少包含在所提供的项目之一中。 我的设置与入门工具包中的设置完全相同,我尝试在 tsconfig 中包含服务器文件夹,但没有成功。

tsconfig.json:
{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"],
      "@/static/*": ["./static/*"]
    }
  },
  "exclude": ["node_modules"],
  "include": ["./src/**/*", "./__helpers__/*"]
}

.eslintrc
{
  "env": {
    "browser": true,
    "es6": true,
    "node": true,
    "jest": true
  },
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": 2019,
    "sourceType": "module"
  },
  "globals": {
    "Atomics": "readonly",
    "SharedArrayBuffer": "readonly"
  },
  "plugins": ["react", "react-hooks", "jsx-a11y", "prettier", "jest"],
  "extends": [
    "plugin:react/recommended",
    "airbnb",
    "prettier",
    "plugin:jest/recommended",
    "plugin:jest/style"
  ],
  "rules": {
    "jest/prefer-strict-equal": "error",
    "jest/prefer-to-have-length": "warn",
    "prettier/prettier": "error",
    "import/prefer-default-export": "off",
    "react/prop-types": "off",
    "react/jsx-filename-extension": "off",
    "react/jsx-props-no-spreading": "off",
    "import/no-extraneous-dependencies": [
      "error",
      {
        "devDependencies": [
          "**/*.test.[jt]s",
          "**/*.spec.[jt]s",
          "**/*.test.[jt]sx",
          "**/*.spec.[jt]sx"
        ]
      }
    ],
    "import/extensions": [
      "error",
      "ignorePackages",
      {
        "ts": "never",
        "tsx": "never",
        "js": "never",
        "jsx": "never"
      }
    ],
    "react/function-component-definition": [
      2,
      {
        "namedComponents": "function-declaration"
      }
    ]
  },
  "overrides": [
    {
      "files": "**/*.+(ts|tsx)",
      "parser": "@typescript-eslint/parser",
      "parserOptions": {
        "project": "./tsconfig.json"
      },
      "plugins": ["@typescript-eslint/eslint-plugin"],
      "extends": [
        "plugin:@typescript-eslint/eslint-recommended",
        "plugin:@typescript-eslint/recommended",
        "prettier"
      ],
      "rules": {
        "@typescript-eslint/explicit-function-return-type": "off",
        "@typescript-eslint/no-explicit-any": "off",
        "@typescript-eslint/no-var-requires": "off",
        "no-use-before-define": [0],
        "@typescript-eslint/no-use-before-define": [1],
        "import/no-unresolved": 0,
        "import/no-extraneous-dependencies": [
          "error",
          {
            "devDependencies": [
              "**/*.test.ts",
              "**/*.spec.ts",
              "**/*.test.tsx",
              "**/*.spec.tsx"
            ]
          }
        ],
        "quotes": "off",
        "@typescript-eslint/quotes": [
          2,
          "backtick",
          {
            "avoidEscape": true
          }
        ],
        "@typescript-eslint/no-unused-vars": [2, { "argsIgnorePattern": "^_" }]
      }
    }
  ],
  "settings": {
    "import/resolver": {
      "typescript": {
        "project": "."
      }
    },
    "react": {
      "version": "detect"
    }
  }
}

server/index.ts:
import express, { Express, Request, Response } from 'express';
import cors from 'cors';

const app: Express = express();
const port = 3000;

app.use(cors());
app.use(express.json());

app.get('/', (req: Request, res: Response) => res.send('Hello World!'));
app.listen(port, () => console.log(`Example app listening on port ${port}!`));

在您的 tsconfig.json 文件中,您需要将服务器文件夹添加到 include:

"include": ["./src/**/*", "./__helpers__/*", "server/**/*" ]