Course contentsShow
← JavaScript
Lesson 2596 of 2,72352. Linting and FormattingFree lesson

Integrating ESLint with Lint-Staged

Configure lint-staged to run ESLint with --fix on staged JavaScript files before committing.

Integrating ESLint with Lint-Staged

You'll learn how to automatically run ESLint on staged files before they're committed, catching errors early and keeping your codebase clean.

What This Integration Accomplishes

Now that you've installed and configured lint-staged (step 2593-2595), you can combine it with ESLint to automatically lint only the files you're about to commit. This is like having a quality checkpoint at the door—nothing gets through without passing inspection, but you only inspect what's actually leaving, not your entire warehouse.

Configuring ESLint in Your Lint-Staged Setup

In your package.json or dedicated configuration file, you map file patterns to ESLint commands. The key is using the --fix flag, which automatically repairs fixable issues before committing:

{
  "lint-staged": {
    "*.js": "eslint --fix"
  }
}

This configuration tells lint-staged: "For any staged .js file, run eslint --fix on it." The --fix flag attempts to automatically correct problems like missing semicolons, incorrect spacing, or unused variables—whatever your ESLint rules define as auto-fixable.

Why the --fix Flag Matters

Without --fix, ESLint would only report errors, and your commit would fail if issues exist. With --fix, ESLint repairs what it can, then re-stages those corrections automatically. It's the difference between a bouncer who just says "you can't enter" versus one who says "let me straighten your tie first."

For multiple file types, you can extend the pattern:

{
  "lint-staged": {
    "*.{js,jsx,ts,tsx}": "eslint --fix"
  }
}

Key Takeaway: Integrating ESLint with lint-staged using the --fix flag ensures only linted, auto-corrected code reaches your repository, preventing broken code from entering your commit history while minimizing manual cleanup.