Lint files staged by git
Last updated 2 months ago by okonet .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install lint-staged 
SYNC missed versions from official npm registry.

???????? lint-staged Build Status for Linux Build Status for Windows npm version Codecov

Run linters against staged git files and don't let :poop: slip into your code base!

???? Help test lint-staged@beta!

Version 10 of lint-staged is coming with changes that help it run faster on large git repositories and prevent loss of data during errors. Please help test the beta version and report any inconsistencies in our GitHub Issues:

Using npm

npm install --save-dev lint-staged@beta

Using yarn

yarn add -D lint-staged@beta

Notable changes

  • A git stash is created before running any tasks, so in case of errors any lost changes can be restored easily (and automatically unless lint-staged itself crashes)
  • Instead of write-tree/read-tree, lint-staged@beta uses git stashes to hide unstaged changes while running tasks against staged files
    • This results in a performance increase of up to 45x on very large repositories
  • The behaviour of committing modifications during tasks (eg. prettier --write && git add) is different. The current version creates a diff of these modifications, and applies it against the original state, silently ignoring any errors. The beta version leaves modifications of staged files as-is, and then restores all hidden unstaged changes as patch. If applying the patch fails due to a merge conflict (because tasks have modified the same lines), a 3-way merge will be retried. If this also fails, the entire commit will fail and the original state will be restored.
    • TL;DR the beta version will never skip committing any changes by tasks (due to a merge conflict), but might fail in very complex situations where unstaged changes cannot be restored cleanly. If this happens to you, we are very interested in a repeatable test scenario.



Linting makes more sense when run before committing your code. By doing so you can ensure no errors go into the repository and enforce code style. But running a lint process on a whole project is slow and linting results can be irrelevant. Ultimately you only want to lint files that will be committed.

This project contains a script that will run arbitrary shell tasks with a list of staged files as an argument, filtered by a specified glob pattern.

Related blogs posts and talks

If you've written one, please submit a PR with the link to it!

Installation and setup

The fastest way to start using lint-staged is to run following command in your terminal:

npx mrm lint-staged

It will install and configure husky and lint-staged depending on code quality tools from package.json dependencies so please make sure you install (npm install --save-dev) and configure all code quality tools like Prettier, ESlint prior that.

Don't forget to commit changes to package.json to share this setup with your team!

Now change a few files, git add or git add --patch some of them to your commit and try to git commit them.

See examples and configuration for more information.


See Releases

Command line flags

$ npx lint-staged --help
Usage: lint-staged [options]

  -V, --version                     output the version number
  -c, --config [path]               Path to configuration file
  -r, --relative                    Pass relative filepaths to tasks
  -x, --shell                       Skip parsing of tasks for better shell support
  -q, --quiet                       Disable lint-staged’s own console output
  -d, --debug                       Enable debug mode
  -p, --concurrent [parallel tasks] The number of tasks to run concurrently, or false to run tasks sequentially
  -h, --help                        output usage information
  • --config [path]: This can be used to manually specify the lint-staged config file location. However, if the specified file cannot be found, it will error out instead of performing the usual search. You may pass a npm package name for configuration also.
  • --relative: By default filepaths will be passed to the linter tasks as absolute. This flag makes them relative to process.cwd() (where lint-staged runs).
  • --shell: By default linter commands will be parsed for speed and security. This has the side-effect that regular shell scripts might not work as expected. You can skip parsing of commands with this option.
  • --quiet: By default lint-staged will print progress status to console while running linters. Use this flag to supress all output, except for linter scripts.
  • --debug: Enabling the debug mode does the following:
    • lint-staged uses the debug module internally to log information about staged files, commands being executed, location of binaries, etc. Debug logs, which are automatically enabled by passing the flag, can also be enabled by setting the environment variable $DEBUG to lint-staged*.
    • Use the verbose renderer for listr.
  • --concurrent [number | (true/false)]: Controls the concurrency of tasks being run by lint-staged. NOTE: This does NOT affect the concurrency of subtasks (they will always be run sequentially). Possible values are:
    • false: Run all tasks serially
    • true (default) : Infinite concurrency. Runs as many tasks in parallel as possible.
    • {number}: Run the specified number of tasks in parallel, where 1 is equivalent to false.


Starting with v3.1 you can now use different ways of configuring it:

  • lint-staged object in your package.json
  • .lintstagedrc file in JSON or YML format
  • lint-staged.config.js file in JS format
  • Pass a configuration file using the --config or -c flag

See cosmiconfig for more details on what formats are supported.

Configuration should be an object where each value is a command to run and its key is a glob pattern to use for this command. This package uses micromatch for glob patterns.

package.json example:

  "lint-staged": {
    "*": "your-cmd"

.lintstagedrc example

  "*": "your-cmd"

This config will execute your-cmd with the list of currently staged files passed as arguments.

So, considering you did git add file1.ext file2.ext, lint-staged will run the following command:

your-cmd file1.ext file2.ext

Filtering files

Linter commands work on a subset of all staged files, defined by a glob pattern. `lint-staged´ uses micromatch for matching files with the following rules:

  • If the glob pattern contains no slashes (/), micromatch's matchBase option will enabled, so globs match a file's basename regardless of directory:
    • "*.js" will match all JS files, like /test.js and /foo/bar/test.js
    • "!(*test).js". will match all JS files, except those ending in test.js, so foo.js but not foo.test.js
  • If the glob pattern does contain a slash (/), it will match for paths as well:
    • "./*.js" will match all JS files in the git repo root, so /test.js but not /foo/bar/test.js
    • "foo/**/\*.js" will match all JS files inside the/foodirectory, so/foo/bar/test.jsbut not/test.js

When matching, lint-staged will do the following

  • Resolve the git root automatically, no configuration needed.
  • Pick the staged files which are present inside the project directory.
  • Filter them using the specified glob patterns.
  • Pass absolute paths to the linters as arguments.

NOTE: lint-staged will pass absolute paths to the linters to avoid any confusion in case they're executed in a different working directory (i.e. when your .git directory isn't the same as your package.json directory).

Also see How to use lint-staged in a multi package monorepo?

Ignoring files

The concept of lint-staged is to run configured linter (or other) tasks on files that are staged in git. lint-staged will always pass a list of all staged files to the task, and ignoring any files should be configured in the task itself.

Consider a project that uses prettier to keep code format consistent across all files. The project also stores minified 3rd-party vendor libraries in the vendor/ directory. To keep prettier from throwing errors on these files, the vendor directory should be added to prettier's ignore configuration, the .prettierignore file. Running npx prettier . will ignore the entire vendor directory, throwing no errors. When lint-staged is added to the project and configured to run prettier, all modified and staged files in the vendor directory will be ignored by prettier, even though it receives them as input.

In advanced scenarios, where it is impossible to configure the linter task itself to ignore files, but some staged files should still be ignored by lint-staged, it is possible to filter filepaths before passing them to tasks by using the function syntax. See Example: Ignore files from match.

What commands are supported?

Supported are any executables installed locally or globally via npm as well as any executable from your $PATH.

Using globally installed scripts is discouraged, since lint-staged may not work for someone who doesn’t have it installed.

lint-staged uses execa to locate locally installed scripts. So in your .lintstagedrc you can write:

  "*.js": "eslint --fix"

Pass arguments to your commands separated by space as you would do in the shell. See examples below.

Starting from v2.0.0 sequences of commands are supported. Pass an array of commands instead of a single one and they will run sequentially. This is useful for running autoformatting tools like eslint --fix or stylefmt but can be used for any arbitrary sequences.

Using JS functions to customize linter commands

When supplying configuration in JS format it is possible to define the linter command as a function which receives an array of staged filenames/paths and returns the complete linter command as a string. It is also possible to return an array of complete command strings, for example when the linter command supports only a single file input.

type LinterFn = (filenames: string[]) => string | string[]

Example: Wrap filenames in single quotes and run once per file

// .lintstagedrc.js
module.exports = {
  '**/*.js?(x)': filenames => => `prettier --write '${filename}'`)

Example: Run tsc on changes to TypeScript files, but do not pass any filename arguments

// lint-staged.config.js
module.exports = {
  '**/*.ts?(x)': () => 'tsc -p tsconfig.json --noEmit'

Example: Run eslint on entire repo if more than 10 staged files

// .lintstagedrc.js
module.exports = {
  '**/*.js?(x)': filenames => (filenames.length > 10 ? 'eslint .' : `eslint ${filenames.join(' ')}`)

Example: Use your own globs

// lint-staged.config.js
const micromatch = require('micromatch')
module.exports = {
  '*': allFiles => {
    const match = micromatch(allFiles, ['*.js', '*.ts'])
    return => `eslint ${file}`)

Example: Ignore files from match

If for some reason you want to ignore files from the glob match, you can use micromatch.not():

// lint-staged.config.js
const micromatch = require('micromatch')
module.exports = {
  '*.js': files => {
    // from `files` filter those _NOT_ matching `*test.js`
    const match = micromatch.not(files, '*test.js')
    return => `eslint ${file}`)

Please note that for most cases, globs can achieve the same effect. For the above example, a matching glob would be !(*test).js.

Example: Use relative paths for commands

const path = require('path')
module.exports = {
  '*.ts': absolutePaths => {
    const cwd = process.cwd()
    const relativePaths = => path.relative(cwd, file))
    return `ng lint myProjectName --files ${relativePaths.join(' ')}`

Reformatting the code

Tools like Prettier, ESLint/TSLint, or stylelint can reformat your code according to an appropriate config by running prettier --write/eslint --fix/tslint --fix/stylelint --fix. After the code is reformatted, we want it to be added to the same commit. This can be done using following config:

  "*.js": ["prettier --write", "git add"]

Starting from v8, lint-staged will stash your remaining changes (not added to the index) and restore them from stash afterwards if there are partially staged files detected. This allows you to create partial commits with hunks using git add --patch. See the blog post


All examples assuming you’ve already set up lint-staged and husky in the package.json.

  "name": "My project",
  "version": "0.1.0",
  "scripts": {
    "my-custom-script": "linter --arg1 --arg2"
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
  "lint-staged": {}

Note we don’t pass a path as an argument for the runners. This is important since lint-staged will do this for you.

ESLint with default parameters for *.js and *.jsx running as a pre-commit hook

  "*.{js,jsx}": "eslint"

Automatically fix code style with --fix and add to commit

  "*.js": ["eslint --fix", "git add"]

This will run eslint --fix and automatically add changes to the commit.

Reuse npm script

If you wish to reuse a npm script defined in your package.json:

  "*.js": ["npm run my-custom-script --", "git add"]

The following is equivalent:

  "*.js": ["linter --arg1 --arg2", "git add"]

Use environment variables with linting commands

Linting commands do not support the shell convention of expanding environment variables. To enable the convention yourself, use a tool like cross-env.

For example, here is jest running on all .js files with the NODE_ENV variable being set to "test":

  "*.js": ["cross-env NODE_ENV=test jest --bail --findRelatedTests"]

Automatically fix code style with prettier for javascript + flow, typescript, markdown or html

  "*.{js,jsx}": ["prettier --write", "git add"]
  "*.{ts,tsx}": ["prettier --write", "git add"]
  "*.{md,html}": ["prettier --write", "git add"]

Stylelint for CSS with defaults and for SCSS with SCSS syntax

  "*.css": "stylelint",
  "*.scss": "stylelint --syntax=scss"

Run PostCSS sorting, add files to commit and run Stylelint to check

  "*.scss": ["postcss --config path/to/your/config --replace", "stylelint", "git add"]

Minify the images and add files to commit

  "*.{png,jpeg,jpg,gif,svg}": ["imagemin-lint-staged", "git add"]
<summary>More about imagemin-lint-staged</summary>

imagemin-lint-staged is a CLI tool designed for lint-staged usage with sensible defaults.

See more on this blog post for benefits of this approach.

Typecheck your staged files with flow

  "*.{js,jsx}": ["flow focus-check", "git add"]

Frequently Asked Questions

Can I use lint-staged via node?


const lintStaged = require('lint-staged')

try {
  const success = await lintStaged()
  console.log(success ? 'Linting was successful!' : 'Linting failed!')
} catch (e) {
  // Failed to load configuration

Parameters to lintStaged are equivalent to their CLI counterparts:

const success = await lintStaged({
  configPath: './path/to/configuration/file',
  shell: false,
  quiet: false,
  debug: false

You can also pass config directly with config option:

const success = await lintStaged({
  config: {
    '*.js': 'eslint --fix'
  shell: false,
  quiet: false,
  debug: false

Using with JetBrains IDEs (WebStorm, PyCharm, IntelliJ IDEA, RubyMine, etc.)

Update: The latest version of JetBrains IDEs now support running hooks as you would expect.

When using the IDE's GUI to commit changes with the precommit hook, you might see inconsistencies in the IDE and command line. This is known issue at JetBrains so if you want this fixed, please vote for it on YouTrack.

Until the issue is resolved in the IDE, you can use the following config to work around it:

husky v1.x

  "husky": {
    "hooks": {
      "pre-commit": "lint-staged",
      "post-commit": "git update-index --again"

husky v0.x

  "scripts": {
    "precommit": "lint-staged",
    "postcommit": "git update-index --again"

Thanks to this comment for the fix!

How to use lint-staged in a multi package monorepo?

Starting with v5.0, lint-staged automatically resolves the git root without any additional configuration. You configure lint-staged as you normally would if your project root and git root were the same directory.

If you wish to use lint-staged in a multi package monorepo, it is recommended to install husky in the root package.json. lerna can be used to execute the precommit script in all sub-packages.

Example repo: sudo-suhas/lint-staged-multi-pkg.

Can I lint files outside of the current project folder?

tl;dr: Yes, but the pattern should start with ../.

By default, lint-staged executes linters only on the files present inside the project folder(where lint-staged is installed and run from). So this question is relevant only when the project folder is a child folder inside the git repo. In certain project setups, it might be desirable to bypass this restriction. See #425, #487 for more context.

lint-staged provides an escape hatch for the same(>= v7.3.0). For patterns that start with ../, all the staged files are allowed to match against the pattern. Note that patterns like *.js, **/*.js will still only match the project files and not any of the files in parent or sibling directories.

Example repo: sudo-suhas/lint-staged-django-react-demo.

Current Tags

  • 10.0.0-beta.15                                ...           beta (9 days ago)
  • 9.5.0                                ...           latest (2 months ago)
  • 10.0.0-1                                ...           next (3 months ago)

131 Versions

  • 10.0.0-beta.15                                ...           9 days ago
  • 10.0.0-beta.14                                ...           25 days ago
  • 10.0.0-beta.13                                ...           a month ago
  • 10.0.0-beta.12                                ...           a month ago
  • 10.0.0-beta.11                                ...           a month ago
  • 10.0.0-beta.10                                ...           a month ago
  • 10.0.0-beta.9                                ...           a month ago
  • 10.0.0-beta.8                                ...           a month ago
  • 10.0.0-beta.7                                ...           a month ago
  • 10.0.0-beta.6                                ...           2 months ago
  • 10.0.0-beta.5                                ...           2 months ago
  • 9.5.0                                ...           2 months ago
  • 10.0.0-beta.4                                ...           2 months ago
  • 10.0.0-beta.3                                ...           2 months ago
  • 10.0.0-beta.2                                ...           2 months ago
  • 10.0.0-beta.1                                ...           2 months ago
  • 9.4.3                                ...           2 months ago
  • 9.5.0-beta.2                                ...           2 months ago
  • 9.5.0-beta.1                                ...           3 months ago
  • 10.0.0-1                                ...           3 months ago
  • 9.4.2                                ...           3 months ago
  • 9.4.1                                ...           4 months ago
  • 10.0.0-0                                ...           4 months ago
  • 9.4.0                                ...           4 months ago
  • 9.3.0                                ...           4 months ago
  • 9.2.5                                ...           5 months ago
  • 9.2.4                                ...           5 months ago
  • 9.2.3                                ...           5 months ago
  • 9.2.2                                ...           5 months ago
  • 9.2.1                                ...           6 months ago
  • 9.2.0                                ...           6 months ago
  • 9.1.0                                ...           6 months ago
  • 9.0.2                                ...           6 months ago
  • 9.0.1                                ...           7 months ago
  • 9.0.0                                ...           7 months ago
  • 8.2.1                                ...           7 months ago
  • 8.2.0                                ...           7 months ago
  • 8.1.7                                ...           8 months ago
  • 8.1.6                                ...           8 months ago
  • 8.1.5                                ...           a year ago
  • 8.1.4                                ...           a year ago
  • 8.1.3                                ...           a year ago
  • 8.1.2                                ...           a year ago
  • 8.1.1                                ...           a year ago
  • 8.1.0                                ...           a year ago
  • 8.0.5                                ...           a year ago
  • 8.0.4                                ...           a year ago
  • 8.0.3                                ...           a year ago
  • 8.0.2                                ...           a year ago
  • 8.0.1                                ...           a year ago
  • 8.0.0                                ...           a year ago
  • 8.0.0-beta.4                                ...           a year ago
  • 8.0.0-beta.3                                ...           a year ago
  • 8.0.0-beta.2                                ...           a year ago
  • 7.3.0                                ...           a year ago
  • 8.0.0-beta.1                                ...           a year ago
  • 8.0.0-beta.0                                ...           a year ago
  • 7.2.2                                ...           a year ago
  • 7.2.1                                ...           a year ago
  • 7.2.0                                ...           2 years ago
  • 7.1.3                                ...           2 years ago
  • 7.1.2                                ...           2 years ago
  • 7.1.1                                ...           2 years ago
  • 7.1.0                                ...           2 years ago
  • 7.0.5                                ...           2 years ago
  • 7.0.4                                ...           2 years ago
  • 7.0.3                                ...           2 years ago
  • 7.0.2                                ...           2 years ago
  • 7.0.1                                ...           2 years ago
  • 7.0.0                                ...           2 years ago
  • 6.1.1                                ...           2 years ago
  • 6.1.0                                ...           2 years ago
  • 6.0.1                                ...           2 years ago
  • 6.0.0                                ...           2 years ago
  • 5.0.0                                ...           2 years ago
  • 4.3.0                                ...           2 years ago
  • 4.2.3                                ...           2 years ago
  • 4.2.2                                ...           2 years ago
  • 4.2.1                                ...           2 years ago
  • 4.2.0                                ...           2 years ago
  • 4.1.3                                ...           2 years ago
  • 4.1.2                                ...           2 years ago
  • 4.1.1                                ...           2 years ago
  • 4.1.0                                ...           2 years ago
  • 4.0.4                                ...           2 years ago
  • 4.0.3                                ...           2 years ago
  • 4.0.2                                ...           3 years ago
  • 4.0.1                                ...           3 years ago
  • 4.0.0                                ...           3 years ago
  • 3.6.1                                ...           3 years ago
  • 3.6.0                                ...           3 years ago
  • 3.5.1                                ...           3 years ago
  • 3.5.0                                ...           3 years ago
  • 3.4.2                                ...           3 years ago
  • 3.4.1                                ...           3 years ago
  • 3.4.0                                ...           3 years ago
  • 3.3.2                                ...           3 years ago
  • 3.3.1                                ...           3 years ago
  • 3.3.0                                ...           3 years ago
  • 3.2.9                                ...           3 years ago
  • 3.2.8                                ...           3 years ago
  • 3.2.7                                ...           3 years ago
  • 3.2.6                                ...           3 years ago
  • 3.2.5                                ...           3 years ago
  • 3.2.4                                ...           3 years ago
  • 3.2.3                                ...           3 years ago
  • 3.2.2                                ...           3 years ago
  • 3.2.1                                ...           3 years ago
  • 3.2.0                                ...           3 years ago
  • 3.1.1                                ...           3 years ago
  • 3.1.0                                ...           3 years ago
  • 4.0.0-beta-2                                ...           3 years ago
  • 4.0.0-beta-1                                ...           3 years ago
  • 3.0.3                                ...           3 years ago
  • 3.0.2                                ...           3 years ago
  • 3.0.1                                ...           3 years ago
  • 2.0.3                                ...           3 years ago
  • 3.0.0-beta1                                ...           3 years ago
  • 3.0.0                                ...           3 years ago
  • 2.0.2                                ...           4 years ago
  • 2.0.1                                ...           4 years ago
  • 2.0.0                                ...           4 years ago
  • 2.0.0-beta.1                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
  • 0.2.2                                ...           4 years ago
  • 0.2.1                                ...           4 years ago
  • 0.2.0                                ...           4 years ago
  • 0.1.1                                ...           4 years ago
  • 0.1.0                                ...           4 years ago
Maintainers (1)
Today 200
This Week 29,497
This Month 81,650
Last Day 5,323
Last Week 34,991
Last Month 155,414
Dependencies (14)
Dependents (856)

Copyright 2014 - 2017 © |