TypeScript ESLint and monorepos. Configuration workaround

We had a couple of users report that they were having problems with Typescript ESLint when the Typescript project was not in the root of the repository.
If you:

  • Use a ESLint configuration file
  • Have your .eslintrc.js file in a subdirectory of your project together with the tsconfig.json file to set up the Typescript compiler
    Chances are that ESLint on Codacy will output file errors like this:
Error on file my_folder/my_file.ts:1. Cause: Parsing error: Cannot read file '/src/tsconfig.json'.

This is because Typescript ESLint searches the tsconfig.json file in the same directory you run ESLint in, which, in the case of Codacy, is the root.

As explained by the Typescript ESLint maintainer here the solution is to use a Javascript ESLint config file (no json or yaml) and add the following to your parserOptions object: tsconfigRootDir: __dirname .

A minimal ESLint .eslintrc.js config file will look like this:

module.exports = {
  parserOptions: {
    project: './tsconfig.json',
    tsconfigRootDir: __dirname,
  },
};
3 Likes