lint-staged
Automatically run Ultracite on your staged Git files with lint-staged
Ultracite seamlessly integrates with lint-staged to automatically format and lint your code before every commit. This ensures that only clean, properly formatted code makes it into your repository.
What is lint-staged?
lint-staged is a tool that runs linters and formatters against staged Git files. It's particularly useful because it:
- Only processes files that are actually being committed
- Prevents poorly formatted code from entering your repository
- Runs quickly since it only checks changed files
- Integrates perfectly with Git hooks
How Ultracite Uses lint-staged
When you run npx ultracite init
, if you select the lint-staged option, Ultracite automatically:
- Installs lint-staged as a development dependency
- Creates or updates your lint-staged configuration
- Configures the formatter to run on relevant file types
The default configuration runs npx ultracite format
on these file types:
- JavaScript/TypeScript files (
.js
,.jsx
,.ts
,.tsx
) - JSON files (
.json
,.jsonc
) - CSS files (
.css
,.scss
) - Markdown files (
.md
,.mdx
)
Supported Configuration Formats
Ultracite's lint-staged integration supports all standard lint-staged configuration formats:
package.json
.lintstagedrc.json
.lintstagedrc.js (CommonJS)
.lintstagedrc.mjs (ES Modules)
.lintstagedrc.yaml
Smart Configuration Merging
If you already have a lint-staged configuration, Ultracite will:
- Detect your existing configuration format
- Merge the Ultracite rules with your existing rules
- Preserve your custom configurations
- Update the file in place
This means you can safely run npx ultracite init
even if you already have lint-staged set up.
How It Works
- Pre-commit Hook: lint-staged runs before each commit via Husky
- File Detection: Only staged files matching the patterns are processed
- Format Command:
npx ultracite format
runs on each matching file - Auto-fixing: Biome automatically fixes issues where possible
- Re-staging: Fixed files are automatically re-staged for commit
Manual Configuration
If you need to customize your lint-staged setup, you can modify the configuration after initialization:
Commands Integration
The lint-staged integration works with Ultracite's command structure:
npx ultracite format
: Runs Biome with auto-fixing (used by lint-staged)npx ultracite lint
: Runs Biome without auto-fixing (for CI/manual checks)
Troubleshooting
Configuration Not Found
If you see errors about missing lint-staged configuration:
ESM/CommonJS Issues
Ultracite automatically detects your project type and creates the appropriate configuration format:
- ESM projects (with
"type": "module"
in package.json) get.mjs
configs - CommonJS projects get
.cjs
configs - Fallback: Creates
.lintstagedrc.json
if module detection fails
Existing Configuration Conflicts
If you have complex existing configurations, you may need to manually merge:
- Back up your existing lint-staged config
- Run
npx ultracite init
- Manually merge any custom rules you need to preserve
Best Practices
- Keep it fast: Only run essential checks in lint-staged
- Use specific patterns: Target only the files that need processing
- Combine with CI: Use lint-staged for quick local checks, full linting in CI
- Test your setup: Make a test commit to ensure everything works
Related
- Husky Integration - Git hooks setup
- Configuration - Biome configuration options
- Setup - Initial project setup