Sub topic 11
Running lint in your editor and in CI
This chapter assumes you know basic TypeScript, npm, Node.js, and the ESLint flat config format. It also assumes your project already has an eslint.config.mjs file and a working tsconfig.json.
Linting is most useful when it runs in two places:
- In your editor, while you write code.
- In continuous integration (CI), before a change is merged.
The editor gives fast feedback. CI gives an independent check that applies to every branch and pull request. Both should use the same project configuration and dependency versions.
Configure editor feedback
Install the ESLint extension for your editor. In Visual Studio Code, the official ESLint extension reads your project configuration and reports problems in TypeScript files.
A typical workflow is:
- Open a TypeScript file.
- Watch for diagnostics, which are messages shown near the offending code.
- Use the editor's quick fix when a rule provides an automatic fix.
- Run the full lint command before committing.
Automatic fixes can change files, so review the resulting diff. Do not assume that every warning can be fixed safely without inspection.
{
"eslint.validate": [
"typescript",
"typescriptreact",
"javascript",
"javascriptreact"
],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
}This example enables ESLint validation for TypeScript and JavaScript. The explicit setting means fixes run when you explicitly save with the configured action, rather than silently changing files during every save configuration.
Your editor can use a different Node.js environment from your terminal. If ESLint works in the terminal but not in the editor, check the editor's selected Node.js runtime, workspace folder, and installed dependencies first.
Use separate check and fix commands
A check command reports problems and returns a failure status when ESLint finds errors:
npx eslint .A fix command applies fixes that rules support:
npx eslint . --fixPut the check command in package.json so every developer and CI job uses the same entry point.
{
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint . --fix"
}
}The npm run lint process exits with status code 0 when linting succeeds. A nonzero status means that ESLint found errors or could not load the configuration. CI uses this status to decide whether a job passes.
Run lint in CI
A CI job should install the exact dependency versions recorded by the lockfile, then run the check-only command. For npm, npm ci is designed for this purpose.
name: lint
on:
pull_request:
push:
branches: [main]
jobs:
eslint:
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 lintDo not use --fix in a normal CI check. A CI job should report a problem rather than modify the checkout and hide the change from the review. Developers can run npm run lint:fix locally, inspect the diff, and commit the result.
Understand the feedback path
Handle failures consistently
If CI reports a lint error, reproduce it with the same command locally. Read the rule name and file location, then decide whether to change the code or add a narrowly scoped exception. If an exception is necessary, use an inline directive with a reason and avoid disabling the rule for the entire project.
Exercise
Why should a normal CI job run npm run lint instead of npm run lint:fix?
Show answer
CI should verify the committed files and return a failure when they violate rules. Automatic fixes belong in a local workflow where the developer can review the changed files.