Search code examples
angulareslintangular-eslint

Ng lint doesn't recognize eslint.config.mjs


I upgraded my project from Angular version 16.2.0 to 18.2.2 and eslint version 8.46 to 9.9.1.

As the documentation suggested I migrated the eslint config file and I got an eslint.config.mjs but now when I try to run ng lint I get this error:

"An unhandled exception occurred: No ESLint configuration found in my-app/src/app"

What other steps should be taken so that the ng lint recognizes the new file?


Solution

  • Currently the Angular ESLint builder for Angular CLI only accepts configs with the js extension, mjs and cjs are unsupported, while ESLint allows those extensions. There is a GitHub issue and an open PR for this feature.

    Until this PR is not merged yet, there are two solutions to the problem:

    1. CommonJS solution

    One solution to the problem is to stick to the js extension and use CommonJS. To import other packages, you need to use require() instead of import, and for exposing the config to ESLint you need to use module.exports instead of export default. This allows you to continue to lint via Angular CLI's ng lint command.

    2. ESM solution

    It is also possible to use the mjs extension, but in this case currently you cannot use the Angular CLI builder for executing the lint command. Instead of running ng lint, run eslint . However if you have multiple projects in your workspace, this solution might not be acceptable, because only the whole repository can be linted, single project linting is not supported.