Lint-Staged

Lint-Staged runs linters only on staged Git files, ensuring that bad code never makes it into your repository—without slowing down your workflow.

Lint-Staged Official Docs

Installation

Install Lint-Staged using the ServerCN CLI:

npx servercn-cli add tooling lint-staged

Configuration

The ServerCN setup automatically adds a lint-staged configuration to your package.json or generates a dedicated .lintstagedrc.json file.

You can customize this configuration to match your project’s structure and tooling.

Basic Example (TypeScript)

package.json
{
  "scripts": {...},
 
  "lint-staged": {
    "*.{js,jsx,ts,tsx}": [
      "eslint --fix",
      "prettier --write"
    ],
    "*.json": [
      "prettier --write"
    ]
  },
 
  "dependencies":{...},
  "devDependencies":{...}
}

Express + TypeScript Example

package.json
{
  "lint-staged":{
    "src/**/*.{ts,tsx}": [
      "eslint --fix",
      "prettier --write"
    ],
    "src/**/*.json": [
      "prettier --write"
    ]
  }
}

With Type Checking (Strict)e

package.json
{
  "lint-staged":{
    "src/**/*.ts": [
      "eslint --fix",
      "prettier --write",
      "tsc --noEmit"
    ]
  }
}

⚠️ Use this with caution on large projects, as type checking may slow down commits.

Using a Separate Config File

Instead of package.json, you can define your rules in .lintstagedrc.json:

.lintstagedrc.json
{
  "*.{js,ts}": [ "eslint --fix", "prettier --write" ],
  "*.{json,md,yml}": "prettier --write"
}

How It Works

  1. You stage your changes with git add.
  2. You run git commit.
  3. Husky triggers the pre-commit hook.
  4. lint-staged runs defined commands only on the files that are about to be committed.
  5. If everything passes, the commit is successful.

Features

  • Efficiency: Only runs linting on changed files, not the whole project.
  • Auto-fix: Can automatically fix formatting issues before committing.
  • Speed: significantly faster than running a full lint check on every commit.

Add More Tooling

npx servercn-cli add tooling commitlint husky eslint prettier lint-staged typescript 

Installation

npx servercn-cli add tooling lint-staged