eslintnrwl-nxeslintrcnrwl

How to ignore .eslintrc.json from NX generator template when linting


I have created a NX plugin/lib named nx.

The plugin's package.json defines the linting target:

 "lint": {
      "executor": "@nrwl/linter:eslint",
      "outputs": ["{options.outputFile}"],
      "options": {
        "lintFilePatterns": [
          "libs/nx/executors.json",
          "libs/nx/package.json",
          "libs/nx/src/executors",
          "libs/nx/src/generators"
        ]
      }
    }

The .eslintrc.json is:

{
  "extends": ["../../.eslintrc.json"],
  "ignorePatterns": ["!**/*"]
}

The extended .eslintrc.json is:

{
  "root": true,
  "ignorePatterns": ["**/*"],
  "plugins": ["@nrwl/nx"],
  "overrides": [
    {
      "files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
      "rules": {
        "@nrwl/nx/enforce-module-boundaries": [
          "error",
          {
            "enforceBuildableLibDependency": true,
            "allow": [],
            "depConstraints": [
              {
                "sourceTag": "*",
                "onlyDependOnLibsWithTags": ["*"]
              }
            ]
          }
        ]
      }
    },
    {
      "files": ["*.ts", "*.tsx"],
      "extends": ["plugin:@nrwl/nx/typescript"],
      "rules": {}
    },
    {
      "files": ["*.js", "*.jsx"],
      "extends": ["plugin:@nrwl/nx/javascript"],
      "rules": {}
    },
    {
      "files": "*.json",
      "parser": "jsonc-eslint-parser",
      "rules": {}
    }
  ]
}

The problem is that the generator dir contains templates for generating ts apps and each app has its own .eslintrc.json file. So when I run linting for some reason it parses these files resulting in an error:

Failed to load config "../../.eslintrc.base.json" to extend from.
Referenced from: 
[...]/libs/nx/src/generators/application/template/.eslintrc.json

I tried to update the ignorePatterns of my config

{
  "ignorePatterns": ["!**/*", "**/*.eslintrc.json"]
}

but without success. How can I solve this problem?


Solution

  • Unfortunately the Nx lint executor does not allow to configure ESLint in that way. However, you can run eslint yourself using the command executor.

    Rreplace your "lint" target with:

    "lint": {
      "command": "eslint --no-eslintrc --config libs/nx/.eslintrc.json <files/directories you want to lint, e.g. "libs/nx">,
      "outputs": ["{options.outputFile}"]
    }
    

    (Though I'd recommend naming your library differently than nx as this will confuse users)