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

Configuring File Patterns

Use glob patterns to target specific file types and run appropriate commands for each pattern.

Configuring File Patterns

You'll learn how to use glob patterns in lint-staged to target specific file types and run the right commands for each.

When you set up lint-staged, you don't want to run the same command on every file that's been staged. You want ESLint for JavaScript, Prettier for JSON, and maybe different rules for test files versus production code. File patterns let you match groups of files and assign specific commands to each group.

Understanding Glob Patterns

Glob patterns are wildcard expressions that match file paths. They work like mini search queries for your filesystem:

  • *.js matches all JavaScript files in the root
  • **/*.js matches JavaScript files in any directory (recursive)
  • src/**/*.{js,jsx} matches JS and JSX files anywhere under src/
  • !**/vendor/** excludes anything in vendor directories

Think of globs as smart filters: they let you slice your codebase into categories so each category gets the right treatment.

Configuring Patterns in lint-staged

In your package.json or dedicated config file, you map patterns to commands:

{
  "lint-staged": {
    "*.js": "eslint --fix",
    "*.{json,md}": "prettier --write",
    "src/**/*.test.js": ["eslint --fix", "npm test -- --findRelatedTests"]
  }
}

Here, staged .js files get linted, JSON and Markdown files get formatted, and test files get both linted and tested. The patterns are evaluated in order, and a file can match multiple patterns.

This targeted approach keeps your pre-commit hooks fast and relevant—you're only running the tools that matter for each file type.

Key Takeaway:
Glob patterns in lint-staged let you route different file types to the appropriate linting, formatting, or testing commands, keeping your workflow efficient and organized.