【问题标题】:Parsing error "parserOptions.project" has been set for @typescript-eslint/parser为@typescript-eslint/parser 设置了解析错误“parserOptions.project”
【发布时间】:2020-11-10 02:27:36
【问题描述】:

我创建了一个新的NestJS project,这是一个非常流行的 NodeJS 框架。但是我的 IDE(PhpStorm 2020.2-Beta)出现了这个错误(见标题),并且 ESLint 根本不起作用。

我使用过 NestJS CLI:

nest new nestjs-micro

我似乎不是唯一遇到此问题的人,因此最好找到此问题的原因并一劳永逸地解决它。

我已经有一个开放的issue但是我没有答案,这真的很成问题。

如果有人知道如何解决问题并保持 ESLint / Prettier 与 PhpStorm 的集成,谢谢。

复制

// .eslintrc.js
module.exports = {
  parser: '@typescript-eslint/parser',
  parserOptions: {
    project: 'tsconfig.json',
    sourceType: 'module',
  },
  plugins: ['@typescript-eslint/eslint-plugin'],
  extends: [
    'plugin:@typescript-eslint/eslint-recommended',
    'plugin:@typescript-eslint/recommended',
    'prettier',
    'prettier/@typescript-eslint',
  ],
  root: true,
  env: {
    node: true,
    jest: true,
  },
  rules: {
    '@typescript-eslint/interface-name-prefix': 'off',
    '@typescript-eslint/explicit-function-return-type': 'off',
    '@typescript-eslint/no-explicit-any': 'off',
  },
};

// tsconfig.json
{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
  }
}

其他信息

版本

Typescript: 3.7.4
Node: 14.3.0
ESLint: 7.1.0
@typescript-eslint/parser: 3.0.2
Yarn: 1.22.4

【问题讨论】:

    标签: node.js phpstorm eslint nestjs prettier


    【解决方案1】:

    我想通了。

    当 Typescript 没有要编译的文件时会发生错误。

    最简单的解决方案是例如创建一个 tsconfig.build.json 文件并在其中指定以下参数:

    {
      "extends": "./tsconfig.json",
      "exclude": ["node_modules", "test", "dist", "dist/**/*spec.ts"],
      "include": ["src/**/*", ".eslintrc.js"]
    }
    

    上面的例子适用于 NestJS,但应该适用于其他项目。

    最令人惊讶的是,它只是在PHPStorm上显示的一个错误,构建,它工作正常。

    【讨论】:

      【解决方案2】:

      FWIW 解决此错误的方法是将 ignorePatterns: ['.eslintrc.js'], 添加到 .eslintrc.js 文件中。这一行告诉eslint 忽略.eslintrc.js 文件(因为它不包含在tsconfig 声明的项目rootDir 中)。

      【讨论】:

      • 这也是允许级联 eslintrc 文件的解决方案,这样我就可以在一个子目录中调整 linting 规则,同时仍然从根配置继承规则。
      【解决方案3】:

      将其添加到 tsconfig.json 中的包含:

      "include": [
        ".eslintrc.js",
      ]
      

      【讨论】:

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