【问题标题】:Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser for gatsby/typescript解析错误:\"parserOptions.project\" has been set for @typescript-eslint/parser for gatsby/typescript
【发布时间】:2022-11-19 23:00:33
【问题描述】:

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

问题是,当我从根目录创建 server/index.ts 文件时,由于它在 src 文件夹之外,在文件的第一行,我得到:解析错误:“parserOptions.project”已为 @typescript 设置-eslint/解析器。该文件与您的项目配置不匹配: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}!`));

【问题讨论】:

    标签: reactjs typescript express gatsby


    【解决方案1】:

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

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

    【讨论】:

    • 我试过了,但没用
    • 您可能需要在更新 tsconfig 文件后清除 eslint 缓存。缓存文件可能存在于 node_modules/.cache 文件夹下
    • 如果像VSCode一样使用IDE,也需要重新加载windows。如果您运行 eslint cli,请确保在命令中删除 --cache,您应该不会再看到该错误
    • 谢谢,重启我的 vscode 就成功了
    【解决方案2】:

    我插入了“include”行并重新加载了 VSCode 的窗口,但无济于事。错误仍然存​​在。还有其他建议吗?

    【讨论】:

      猜你喜欢
      • 2020-11-10
      • 2021-11-01
      • 2021-06-13
      • 2020-02-18
      • 2021-01-24
      • 2020-12-31
      • 2021-07-27
      • 2021-08-24
      • 2020-11-30
      相关资源
      最近更新 更多