Skip to content

Guide · Open source

How to use lint-staged: setup, config and common tips

Updated · written with AI from the project's own documentation and checked against our catalogue data

lint-staged is a JavaScript tool that runs tasks such as formatters and linters against the files you have staged in git. It is usually connected to the pre-commit step so problems in those files are caught before the commit is made. This guide shows you how to install it, configure it and run it, so you can check only the files you are about to commit.

lint-stagedlint-staged/lint-staged · ★ 14.8k · MIT

Quick start

  1. 1Install lint-staged. Run npm install --save-dev lint-staged in your project. Make sure Node.js, npm and git are available.
  2. 2Set up a pre-commit hook. Use Husky or another git hook tool so the pre-commit step runs lint-staged.
  3. 3Install your checkers. Add tools such as ESLint or Prettier to the project.
  4. 4Add a configuration. Map file patterns to commands, for example { "*.js": "eslint" }.
  5. 5Stage and commit. Use git add on a few files, then run git commit to see lint-staged run on the staged files.
  6. 6Commit the setup. Commit package.json and the hook folder so your team uses the same checks.

Why run checks on staged files only?

Formatters and linters are most useful when they run before you commit, because that keeps errors out of the repository and enforces a consistent code style. Running them on a whole project can be slow, and opinionated tasks can produce results for files you are not changing.

lint-staged filters tasks down to the files that will be committed. The README describes it as a script that runs shell tasks with a list of staged files as an argument, filtered by a glob pattern.

Install lint-staged in your project

You need Node.js, npm and git. Install lint-staged as a dev dependency with the following command. The README notes that it requires further setup before it does anything useful.

  • Run the install command in your project folder.
  • Commit the changes to package.json so teammates get the same setup.
npm install --save-dev lint-staged

Set up a pre-commit git hook

lint-staged is meant to run on the pre-commit git hook. The README recommends Husky as a popular way to configure git hooks, and links to the git documentation on hooks if you want to learn more.

Also install the tools you want to run, such as ESLint or Prettier. lint-staged runs them; it does not replace them.

  • Install and configure Husky, or another hook manager such as lefthook or simple-git-hooks.
  • Make sure the pre-commit hook calls lint-staged.
  • Commit the .husky folder so the setup is shared with your team.

Configure lint-staged to run your tools

Configuration maps file patterns to commands. The README gives this example, which runs ESLint on all staged JavaScript files. Each key is a glob pattern and each value is the command to run on the matching staged files.

See the configuration and filtering sections of the README for more options, including JS configuration files and running several commands in sequence.

{ "*.js": "eslint" }

Test it with a commit

Change a few files, stage some of them with git add (or git add --patch), and run git commit. lint-staged runs your configured tasks against the staged files and reports the result. The README example shows Prettier running on staged JSON and Markdown files.

lint-staged backs up your working state with a git stash before running tasks. The README warns that it runs git operations that affect your files, so review changes after a failed run.

  • Only staged files are checked by default.
  • Use npx lint-staged --help to see all command line flags.
npx lint-staged --help

Useful command line flags

The README lists flags for changing how lint-staged runs. Some common ones are --concurrent to control how many tasks run in parallel, --quiet to hide lint-staged's own output, --verbose to show task output even when tasks succeed, and --debug to log extra information.

The --all flag runs tasks on all files tracked in git instead of only staged ones. The README says it is useful for running on a clean state, for example in CI.

  • --concurrent: set number of parallel tasks, or false for serial.
  • --config: point to a specific config file, or read it from stdin with -.
  • --continue-on-error: run all tasks even if one fails.
  • --no-stash: turn off the automatic backup stash.

What people use it for

  • Running ESLint on staged JavaScript files before each commit.
  • Running Prettier on staged JSON and Markdown files so formatting stays consistent.
  • Blocking commits that have linting errors in the files being changed.
  • Running checks on a clean state in CI with the --all flag.

Frequently asked questions

Is lint-staged free to use?

lint-staged is released under the MIT licence, as stated in the project listing. Check the LICENSE file in the repository for the full terms.

What do I need to install lint-staged?

The project lists Node.js, npm and git as prerequisites. Install it with npm install --save-dev lint-staged, then set up a git hook to run it.

Does lint-staged work without git?

No. lint-staged works on files staged in git, so it is not suitable for projects that do not use git.

Do I need Husky to use lint-staged?

The README recommends Husky as a popular choice for git hooks, but it is not the only option. Comparable projects listed include lefthook, pre-commit and simple-git-hooks.

What is a good alternative if I only want to check files without editing them?

The README points to lint-staged.sh, a simpler shell script for checking staged files without automatic edits.

Why did my commit change files I did not expect?

Tasks such as Prettier --write edit files. The README shows Prettier writing changes and staging them, so review the diff and configure tasks to only check when you do not want edits.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

lint-staged suits JavaScript or Node.js projects that already use ESLint or Prettier and want fast, consistent checks on the files being committed. Skip it if your project does not use git, you only want checks without automatic edits, or your team does not want to maintain git hooks and config, in which case lint-staged.sh or a simpler setup may fit better.

Install options & downloads