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

Integrating Prettier with Lint-Staged

Add Prettier to lint-staged to auto-format staged files, ensuring consistent code style.

Integrating Prettier with Lint-Staged

You'll learn how to automatically format your staged files with Prettier before committing, ensuring every commit has consistent code style.

Why Add Prettier to Lint-Staged?

You've already integrated ESLint with lint-staged to catch code quality issues. Now it's time to add Prettier to the same workflow. Think of it like having both a grammar checker (ESLint) and a style guide editor (Prettier) review your work before it goes public. By running Prettier through lint-staged, you guarantee that only beautifully formatted code makes it into your repository—no manual formatting needed.

Setting Up Prettier in Lint-Staged

In your package.json or separate lint-staged configuration file, add Prettier alongside your ESLint command:

{
  "lint-staged": {
    "*.js": [
      "eslint --fix",
      "prettier --write"
    ]
  }
}

The order matters here: ESLint runs first to fix linting issues, then Prettier reformats the code. Both commands automatically stage the changes they make, so the corrected and formatted versions are what actually get committed.

Common Pattern for Multiple File Types

You might want Prettier to handle more than just JavaScript:

{
  "lint-staged": {
    "*.{js,json,md}": "prettier --write",
    "*.js": "eslint --fix"
  }
}

This configuration runs Prettier on JavaScript, JSON, and Markdown files, while ESLint only checks JavaScript files.

How It Works Together

When you commit staged files, lint-staged intercepts them, runs Prettier to reformat, stages the formatted versions, and then proceeds with the commit. You never have to remember to run npm run format manually again.

Key Takeaway: Adding prettier --write to your lint-staged configuration ensures all committed code is automatically formatted, creating a zero-effort style consistency system for your entire team.