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.
Installation
Install Lint-Staged using the ServerCN CLI:
npx servercn-cli add tooling lint-stagedConfiguration
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
Express + TypeScript Example
package.json
With Type Checking (Strict)e
package.json
⚠️ 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
How It Works
- You stage your changes with
git add. - You run
git commit. - Husky triggers the
pre-commithook. lint-stagedruns defined commands only on the files that are about to be committed.- 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