Sub topic 11
Running lint in your editor and in CI
This chapter assumes you have a working eslint.config.mjs that uses typescript-eslint, as built in the earlier chapters. It also assumes you can read a package.json and a basic CI workflow file. Your config is only useful if it runs at the right moments. Run it while you type, so you catch problems early. Run it again on every pull request, so nothing slips past.
One config, three places
The same eslint.config.mjs should drive every place lint runs. If the editor, the pre-commit hook and CI each use different settings, developers see warnings that CI ignores, or CI rejects code that looked clean locally.
The editor is the fastest feedback and the easiest to bypass. CI is the slowest and cannot be bypassed. Treat CI as the authority and the editor as a convenience.
Linting in the editor
Visual Studio Code
Install the ESLint extension (published by Microsoft). It does not bundle ESLint. It loads the eslint package from your project's node_modules, so the version your team installs is the version the editor uses. If the extension reports that it cannot find ESLint, run npm install first.
Current versions of the extension detect eslint.config.mjs automatically. Older versions need flat config mode switched on. Add this to .vscode/settings.json:
{
"eslint.useFlatConfig": true,
"eslint.workingDirectories": [{ "mode": "auto" }],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
}The three settings do the following:
eslint.useFlatConfigtells older extension versions to look foreslint.config.*instead of.eslintrc.eslint.workingDirectorieswithautopicks the directory of the nearest config file for each file you open. This matters in monorepos, where each package may have its own config.source.fixAll.eslintundereditor.codeActionsOnSaveapplies automatic fixes whenever you save. The valueexplicitmeans the fixes run on a manual save, not on autosave.
Committing .vscode/settings.json gives the whole team the same behavior.
Other editors
JetBrains IDEs (WebStorm, IntelliJ IDEA) have a built-in ESLint setting. Choose automatic configuration and enable "Run eslint --fix on save" if you want fixes applied on save. Neovim users usually run the eslint language server from vscode-langservers-extracted through nvim-lspconfig. All of these read the same flat config file.
Type-aware rules in the editor
Type-checked rules need parserOptions.projectService: true and a tsconfigRootDir, as set up in the type-aware chapter. The editor extension must be able to resolve that tsconfigRootDir. Using import.meta.dirname makes the path independent of the editor's working directory, which is why the earlier example uses it.
Type-aware linting in the editor is slower than syntax-only linting. The first diagnostics after opening a project may take several seconds while TypeScript builds its program.
Common editor problems
| Symptom | Likely cause | Fix |
|---|---|---|
| No squiggles at all | Extension cannot find `eslint` or the config | Run `npm install`, open the ESLint output panel, check for errors |
| Squiggles differ from CI | Editor uses a different ESLint version or working directory | Check the version in the output panel and set `eslint.workingDirectories` |
| "file not found by the project service" error | The file is not covered by any `tsconfig.json` | Include the file in a tsconfig, or disable type-checked rules for it with `disableTypeChecked` |
| Edits to the config are ignored | The ESLint server cached the old config | Run "ESLint: Restart ESLint Server" from the command palette |
| Fixes apply to the wrong lines on save | Another formatter runs on save too | Keep formatting in a separate tool and set an explicit order, as covered in the formatting chapter |
The second row is the most frequent. When the editor and CI disagree, compare the ESLint version printed in the extension's output panel with the one from npx eslint --version.
Linting in CI
Scripts
Put the commands in package.json so developers and CI run exactly the same thing:
{
"scripts": {
"lint": "eslint . --max-warnings 0",
"lint:fix": "eslint . --fix",
"typecheck": "tsc --noEmit"
}
}The flag --max-warnings 0 makes warnings fail the run. Without it, ESLint exits successfully when only warnings exist, and warnings pile up until nobody reads them. Treating warnings as failures in CI keeps the warning level useful as a "fix this soon" signal during local work while still enforcing a clean result before merge.
CI should not run --fix. A fix applied inside a throwaway CI checkout is discarded, and the job would pass without anything being corrected in the repository. Let CI only report.
Exit codes
CI systems decide pass or fail from the process exit code. ESLint uses three:
| Exit code | Meaning |
|---|---|
| 0 | No errors, and warnings are within `--max-warnings` |
| 1 | At least one error, or warnings exceeded `--max-warnings` |
| 2 | Configuration problem or internal failure, such as a missing config or a crash |
Exit code 2 deserves attention. A broken config or a type-aware rule that cannot find a tsconfig does not look like a lint error. It looks like a crashed job, and the log message is the thing to read.
A GitHub Actions workflow
name: lint
on:
pull_request:
push:
branches: [main]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm run lint
- run: npm run typecheckTwo details matter here. First, npm ci installs the exact versions in package-lock.json, which keeps the CI ESLint version identical to what developers have. Second, the job runs tsc --noEmit as a separate step. typescript-eslint reports lint problems, not compile errors, so ESLint passing says nothing about whether the code type-checks. Run both.
Caching
eslint . --cache stores results in a .eslintcache file and skips files that have not changed. That speeds up local runs and pre-commit hooks. Add .eslintcache to .gitignore.
npx eslint . --cache --cache-location node_modules/.cache/eslint/Reports
For tooling that consumes results, write a machine-readable report while still failing the job on errors:
npx eslint . --format json --output-file eslint-report.jsonThe command still exits with code 1 when errors exist, so the job fails and you can upload the file as an artifact afterward.
Stale suppressions
Flat config reports inline disable comments that no longer suppress anything. You can control it explicitly:
// additional entry in the flat config array
{
linterOptions: {
reportUnusedDisableDirectives: 'error',
},
},This keeps old eslint-disable comments from lingering after the code they covered is fixed. Combined with the required reason text from the earlier chapter, it keeps suppressions honest.
Pre-commit hooks
A pre-commit hook catches problems before they reach a pull request. Linting the whole repository on every commit is slow, so tools such as lint-staged pass only the staged files to ESLint. Be aware that type-aware rules on a small set of files still load the TypeScript project, so the saving is smaller than with syntax-only rules. Because hooks can be skipped with git commit --no-verify, they supplement CI and never replace it.
Choosing where each check runs
- Editor: every rule you have enabled, with fix on save for safe fixes.
- Pre-commit: staged files only, cached, with fixes applied.
- CI: the full repository, uncached, no fixes, warnings as failures, plus
tsc --noEmit.
Exercise
Your CI lint job passes, but a teammate sees a @typescript-eslint/no-floating-promises error in VS Code on the same commit. Name two causes you would check first.
Show answer
First, version mismatch: the extension may be loading a different ESLint or typescript-eslint version than CI, or CI may have installed different versions without npm ci. Compare the version in the ESLint output panel with npx eslint --version. Second, working directory or tsconfig resolution: the editor may resolve a different config or tsconfig root than CI, so check eslint.workingDirectories and that tsconfigRootDir uses import.meta.dirname. A third possibility is that the CI script excludes the file through ignores or a narrower path than ..
Exercise
A job runs npx eslint . --fix in CI and always goes green, yet violations remain in the repository. Why, and what should the command be?
Show answer
--fix repairs the files inside the temporary CI checkout, which is discarded, so remaining problems are fixed away before the exit code is computed and nothing changes in the repository. CI should only report: use npx eslint . --max-warnings 0 (through npm run lint) so that any error or warning fails the job, and apply fixes locally with npm run lint:fix.