Sub topic 12
Performance and monorepo considerations
This chapter assumes you already have a working eslint.config.mjs with a type-checked preset and the project service enabled. It also assumes you can read a tsconfig.json, including include, exclude, and references. The goal is to explain why type-aware linting costs more than plain linting, how to measure that cost, and how to set up one repository that holds several packages.
Why type-aware linting is slower
A non-type-aware rule only needs the syntax tree of the file it is checking. The parser reads the file, the rules walk the tree, and the work stays proportional to that one file.
A type-checked rule such as @typescript-eslint/no-floating-promises asks a different question: "what is the type of this expression?" Answering it requires a TypeScript program. The program must load your tsconfig.json, read every file that the file imports, including declarations in node_modules, and build the type information. This is the same work tsc does, and it runs on top of the normal lint pass.
The cost has three parts:
- Startup. The first file linted pays for building the program. In a large codebase this can take several seconds.
- Memory. The program stays in memory while linting continues. Large projects may need a bigger Node heap.
- Per-file work. Each file must be parsed again, and the program must be updated when a file changes. In an editor this happens on every keystroke or save.
Linting with only recommended is often several times faster than the same run with recommendedTypeChecked. Measure your own project before you decide how much effort to spend here.
Measure before you tune
ESLint has built-in timing output. Set the TIMING environment variable and it prints the slowest rules after the run:
TIMING=1 npx eslint .This syntax works in POSIX shells. On Windows, set the variable with your shell's own syntax or use a tool such as cross-env.
The report lists each rule with the time spent and its share of the total. Type-checked rules usually appear near the top, but the report also shows slow third-party rules. Rules from import-analysis plugins are a frequent cause, because they read many files. Fix the biggest line first. A rule that takes 2% of the time is not worth a config change.
To see what typescript-eslint itself is doing, such as which tsconfig it picked for a file, turn on its debug logging:
DEBUG=typescript-eslint:* npx eslint src/index.tsUse the project service correctly
The projectService option is the recommended way to get type information. For each file, it asks TypeScript's own language-service machinery which project the file belongs to. It looks for the nearest tsconfig.json in the file's directory or a parent directory, and it reuses programs between files. The older parserOptions.project option with a list of globs made typescript-eslint resolve those globs itself. That was slower and more fragile, so use projectService for new setups.
// eslint.config.mjs
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import { defineConfig } from 'eslint/config';
export default defineConfig(
{ ignores: ['**/dist/**', '**/coverage/**'] },
eslint.configs.recommended,
{
files: ['**/*.ts', '**/*.tsx'],
extends: [tseslint.configs.recommendedTypeChecked],
languageOptions: {
parserOptions: {
projectService: {
allowDefaultProject: ['vitest.config.ts'],
},
tsconfigRootDir: import.meta.dirname,
},
},
},
{
files: ['**/*.js', '**/*.mjs', '**/*.cjs'],
extends: [tseslint.configs.disableTypeChecked],
},
);Three details in this config affect performance and correctness:
- Type-checked rules are scoped to TypeScript files. The
filespattern keeps them away from JavaScript files. The second entry callsdisableTypeCheckedfor JavaScript files, so they never ask for a program. Plain JavaScript files such aseslint.config.mjsare the usual cause of the error "was not found by the project service." tsconfigRootDiris set. Without it, relative paths resolve against the directory you started ESLint from. Running lint from a subfolder can then pick the wrong tsconfig.allowDefaultProjectlists a file that your tsconfig does not include. Such files are linted with a default program that uses the compiler's default options. The option accepts only a small number of files and only simple globs:*matches in one directory, and**is not allowed. This is deliberate. Each default-project file is expensive, and typescript-eslint caps the count to stop you from putting a whole folder there. If you need many files, add them to a real tsconfig instead.
Make sure every linted file is in a tsconfig
A common surprise is that your build tsconfig excludes tests, so type-aware linting fails on *.test.ts. The project service needs the file to belong to some project. Two ways to solve this:
- Split the configs. Keep a
tsconfig.jsonthat includes everything (source and tests) and is used by the editor, the linter, andtsc --noEmit. Add atsconfig.build.jsonthat extends it and excludes tests, and point your build at that file. - Use a solution-style root. A root
tsconfig.jsonwith"files": []and areferencesarray lists the real projects. TypeScript's language service finds the referenced project that includes a given file, so the project service can use this layout.
The first option is simpler and works the same in every editor, so prefer it unless you already use project references.
Monorepos
A monorepo has several packages, each with its own tsconfig.json. The project service handles this well, because it chooses the tsconfig per file instead of once per run.
repo/
eslint.config.mjs
package.json
packages/
api/
tsconfig.json
src/
web/
tsconfig.json
src/You have two layouts to choose from.
One config at the root. The config above sits at repo/eslint.config.mjs, and you run npx eslint . from the root. ESLint finds the same config for every file, and the project service picks packages/api/tsconfig.json for files under api and packages/web/tsconfig.json for files under web. Set tsconfigRootDir to the repo root. This layout is easy to keep consistent, but one slow package slows every run.
A config per package. Each package has its own eslint.config.mjs and a lint script, and the task runner (Turborepo, Nx, or your package manager's workspace command) runs them in parallel. Set tsconfigRootDir: import.meta.dirname in each package's config so that the root points at that package. This layout lets you cache results per package and skip packages that did not change. Its cost is some duplication. Put the shared parts in a small internal package that exports a config array, and import it in each package's config file.
flowchart TDBecause that block is only a stub, here is the diagram in the required form:
Ways to reduce lint time
| Technique | Effect | Trade-off |
|---|---|---|
| Scope type-checked presets to `**/*.ts` and call `disableTypeChecked` for JavaScript | Files that do not need types never build a program | Needs a `files` pattern that matches your layout |
| `--cache` with a persisted cache file | Skips files whose content and config did not change | Can be stale for type-aware rules, see the warning below |
| Per-package lint tasks with a task runner | Runs packages in parallel and skips unchanged ones | Config shared through an internal package |
| Lint only changed files in pre-commit hooks | Fast feedback on small changes | Still pays startup cost; keep full runs in CI |
| Remove or replace slow third-party rules | Often the largest single win | You lose those checks |
| Raise the Node heap, for example `NODE_OPTIONS=--max-old-space-size=4096` | Avoids out-of-memory crashes on large programs | Does not make runs faster |
| `--concurrency` (ESLint 9.34 and later) | Lints files across several threads | Each thread builds its own program, so memory grows |
Enable the cache with:
npx eslint . --cache --cache-location node_modules/.cache/eslint/.eslintcacheCommon errors in large setups
- "was not found by the project service." The file is not in any tsconfig
include. Add it to a tsconfig, add it toallowDefaultProjectif it is one of a few config files, or turn off type-checked rules for it. - Too many default-project files. You listed a broad glob in
allowDefaultProject. Move those files into a tsconfig. - Wrong tsconfig chosen. Two nested tsconfigs exist and the file belongs to the nearer one, which excludes it. Check
includeandexcludein the nearest file. - Out-of-memory crash. Raise the heap size, or split lint into per-package tasks so each process holds one package.
Exercise
A monorepo has packages/api/tsconfig.json, and a developer adds packages/api/scripts/seed.ts. The api tsconfig has "include": ["src"]. After that, npx eslint . reports that seed.ts was not found by the project service. Give two fixes, and say which one you would choose if the team expects to add many files under scripts/.
Show answer
Fix 1: add "scripts" to the include array in packages/api/tsconfig.json so the file belongs to a project. Fix 2: list the file in projectService.allowDefaultProject, for example ['packages/api/scripts/seed.ts']. With many files expected under scripts/, choose fix 1. The allowDefaultProject list is limited to a few files, does not allow **, and each default-project file slows linting. A third option is to turn off type-checked rules for that folder with disableTypeChecked, but that removes useful checks.
Exercise
Your CI lint job takes ten minutes. You run TIMING=1 npx eslint . and see that one third-party import-analysis rule takes 40% of the time, and @typescript-eslint/no-floating-promises takes 8%. What do you tackle first, and why? Name one risk of enabling --cache in CI for this repository.
Show answer
Tackle the third-party rule first, because it accounts for the largest share, and removing or replacing it gives the biggest saving. no-floating-promises is a high-value rule and a small cost, so keep it. The cache risk is that type-aware results for a file can depend on other files. A cached result may be stale after a change elsewhere, so a real error can be hidden. Run CI without the cache, or key it on a hash of all source files.