简体   繁体   English

在干净的 Angular 项目中运行“ng lint”时,eslint 会为另一个文件夹中完全不同的项目提供错误

[英]When running "ng lint" in a clean Angular project eslint gives errors for a completely different project in another folder

I have a very strange issue with Eslint and Angular.我对 Eslint 和 Angular 有一个非常奇怪的问题。 Every time I run ng lint I get errors such as:每次我运行ng lint时,都会出现以下错误:

An unhandled exception occurred: Cannot read config file: [COMPLETELY_DIFFERENT_PROJECT_PATH]\node_modules\eslint-config-prettier@typescript-eslint.js Error: "prettier/@typescript-eslint" has been merged into "prettier" in eslint-config-prettier 8.0.0.发生未处理的异常:无法读取配置文件:[COMPLETELY_DIFFERENT_PROJECT_PATH]\node_modules\eslint-config-prettier@typescript-eslint.js 错误:“prettier/@typescript-eslint”已合并到 eslint-config-prettier 中的“prettier”中8.0.0。 See: https://github.com/prettier/eslint-config-prettier/blob/main/CHANGELOG.md#version-800-2021-02-21请参阅: https ://github.com/prettier/eslint-config-prettier/blob/main/CHANGELOG.md#version-800-2021-02-21
Referenced from: [COMPLETELY_DIFFERENT_PROJECT_PATH]\node_modules@nestjs\axios.eslintrc.js See "[USER]\AppData\Local\Temp\ng-H0X9RR\angular-errors.log" for further details.引用自:[COMPLETELY_DIFFERENT_PROJECT_PATH]\node_modules@nestjs\axios.eslintrc.js 有关详细信息,请参阅“[USER]\AppData\Local\Temp\ng-H0X9RR\angular-errors.log”。

Note that these errors occur in a completely different project and a completely different folder than the project I'm running the ng lint command from.请注意,这些错误发生在与我运行ng lint命令的项目完全不同的项目和文件夹中。

To verify this issue I created a completely new and fresh Angular project using为了验证这个问题,我创建了一个全新的 Angular 项目,使用

ng new新的

And then add eslint schematics using然后使用添加eslint原理图

ng add @angular-eslint/schematics ng 添加@angular-eslint/schematics

This generates the following project files:这会生成以下项目文件:

.eslintrc.json .eslintrc.json

{
  "root": true,
  "ignorePatterns": [
    "projects/**/*"
  ],
  "overrides": [
    {
      "files": [
        "*.ts"
      ],
      "parserOptions": {
        "project": [
          "tsconfig.json"
        ],
        "createDefaultProgram": true
      },
      "extends": [
        "plugin:@angular-eslint/recommended",
        "plugin:@angular-eslint/template/process-inline-templates"
      ],
      "rules": {
        "@angular-eslint/directive-selector": [
          "error",
          {
            "type": "attribute",
            "prefix": "app",
            "style": "camelCase"
          }
        ],
        "@angular-eslint/component-selector": [
          "error",
          {
            "type": "element",
            "prefix": "app",
            "style": "kebab-case"
          }
        ]
      }
    },
    {
      "files": [
        "*.html"
      ],
      "extends": [
        "plugin:@angular-eslint/template/recommended"
      ],
      "rules": {}
    }
  ]
}

package.json包.json

{
  "name": "new-angular-project",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test",
    "lint": "ng lint"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~13.3.0",
    "@angular/common": "~13.3.0",
    "@angular/compiler": "~13.3.0",
    "@angular/core": "~13.3.0",
    "@angular/forms": "~13.3.0",
    "@angular/platform-browser": "~13.3.0",
    "@angular/platform-browser-dynamic": "~13.3.0",
    "@angular/router": "~13.3.0",
    "rxjs": "~7.5.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~13.3.5",
    "@angular-eslint/builder": "13.2.1",
    "@angular-eslint/eslint-plugin": "13.2.1",
    "@angular-eslint/eslint-plugin-template": "13.2.1",
    "@angular-eslint/schematics": "13.2.1",
    "@angular-eslint/template-parser": "13.2.1",
    "@angular/cli": "~13.3.5",
    "@angular/compiler-cli": "~13.3.0",
    "@types/jasmine": "~3.10.0",
    "@types/node": "^12.11.1",
    "@typescript-eslint/eslint-plugin": "5.17.0",
    "@typescript-eslint/parser": "5.17.0",
    "eslint": "^8.12.0",
    "jasmine-core": "~4.0.0",
    "karma": "~6.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.1.0",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "~1.7.0",
    "typescript": "~4.6.2"
  }
}

angular.json角.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "newAngularProject": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        },
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/new-angular-project",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "newAngularProject:build:production"
            },
            "development": {
              "browserTarget": "newAngularProject:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "newAngularProject:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-eslint/builder:lint",
          "options": {
            "lintFilePatterns": [
              "src/**/*.ts",
              "src/**/*.html"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "newAngularProject",
  "cli": {
    "defaultCollection": "@angular-eslint/schematics"
  }
}

But even for a completely clean project running ng lint still gives me errors about files that are in a completely different folder than the project I'm running it from.但即使对于一个完全干净的项目,运行ng lint仍然会给我关于与我正在运行它的项目完全不同的文件夹中的文件的错误。 I already searched high and low for a solution but I can't seem to find any solution to this.我已经从高处和低处搜索了一个解决方案,但我似乎找不到任何解决方案。

What I also find strange is that the eslint extension in VSCode for detecting linting issues while you're programming does work: https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint我还觉得奇怪的是 VSCode 中用于在编程时检测 linting 问题的 eslint 扩展确实有效: https ://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint

I'm completely stumped by this.我完全被这个难住了。 Is there anyone who can help me?有谁能帮助我吗?

Try to specify the working directory eslint --ext .js,.ts .尝试指定工作目录eslint --ext .js,.ts . the . . or, ng lint --files=src/**/*.ts .或者, ng lint --files=src/**/*.ts You can add this to package.json as: lint:eslint --ext .js,.ts .您可以将其添加到package.json中: lint:eslint --ext .js,.ts . and run npm run lint并运行npm run lint

Found the complete solution based on Idan Rokach's answer.根据 Idan Rokach 的回答找到了完整的解决方案。 I ended up adding "lint": "eslint --ext .js,.ts src" to package.json to only perform linting on the src folder.我最终将"lint": "eslint --ext .js,.ts src"添加到package.json以仅对src文件夹执行 linting。 Still not sure what the underlying root cause was, but at least this works.仍然不确定根本原因是什么,但至少这是可行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 angular 项目中 ng lint 期间未处理的异常 - Unhandled exception during ng lint in angular project 不确定如何在角度项目中使用 ng lint - Not sure how to use ng lint in angular project 如何在 angular 项目中调试 ng lint broken - How to debug ng lint broken in angular project Angular 8:在运行 ng build --prod --base-href './project1' 时访问资产文件夹 - Angular 8: access the assets folder when running ng build --prod --base-href './project1' 无法在我的angular-cli项目上运行ng lint - Can't run ng lint on my angular-cli project 使用 angular 10 排除 LINT 中的参考库项目文件夹 - Exclude the reference library project folder in LINT with angular 10 我已将 angular 项目从一台计算机迁移到另一台计算机,当我运行“npm i”时,它给了我这些错误 - i have migrated angular project from one computer to another and when I run `npm i` it gives me these errors 尝试对 Angular 模板进行 lint 时出现 ESLint 错误 - ESLint error when trying to lint Angular templates 在Angular中用多个项目迁移TSLint到ESLint,ng lint命令挂起 - Migrate TSLint to ESLint in Angular with multiple projects, ng lint command hangs 当从angular文档下载项目并运行ng serve -o项目定义时找不到 - when download project from angular docs and running ng serve -o project definition could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM