eslint-prettier-husky-config
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseESLint, Prettier, Husky Configuration
ESLint、Prettier、Husky 配置
Overview
概述
Configure comprehensive code quality tooling for Next.js/React projects using ESLint v9 (flat config), Prettier, Husky git hooks, lint-staged, and CI lint checks.
为Next.js/React项目配置全套代码质量工具,包括ESLint v9(扁平配置)、Prettier、Husky Git钩子、lint-staged,以及CI代码检查。
Installation and Configuration Steps
安装与配置步骤
1. Install Dependencies
1. 安装依赖包
Install required packages for ESLint v9, Prettier, and git hooks:
bash
npm install -D eslint@^9 @eslint/js eslint-config-prettier eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-jsx-a11y prettier husky lint-stagedFor TypeScript projects, add:
bash
npm install -D @typescript-eslint/parser @typescript-eslint/eslint-plugin typescript-eslint安装ESLint v9、Prettier和Git钩子所需的包:
bash
npm install -D eslint@^9 @eslint/js eslint-config-prettier eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-jsx-a11y prettier husky lint-staged对于TypeScript项目,额外安装:
bash
npm install -D @typescript-eslint/parser @typescript-eslint/eslint-plugin typescript-eslint2. Create ESLint Flat Config
2. 创建ESLint扁平配置文件
Create in project root using the provided template from . This flat config format:
eslint.config.mjsassets/eslint.config.mjs- Uses modern ESLint v9 configuration
- Includes React, React Hooks, and JSX accessibility rules
- Supports TypeScript with type-aware linting
- Ignores Next.js build directories and configuration files
Customize the configuration based on project needs:
- Adjust for different ECMAScript versions
languageOptions.parserOptions - Modify to match team preferences
rules - Add additional plugins as needed
在项目根目录创建,使用中的模板。该扁平配置格式:
eslint.config.mjsassets/eslint.config.mjs- 采用现代化ESLint v9配置
- 包含React、React Hooks和JSX可访问性规则
- 支持带类型检查的TypeScript
- 忽略Next.js构建目录和配置文件
根据项目需求自定义配置:
- 调整以适配不同ECMAScript版本
languageOptions.parserOptions - 修改以匹配团队偏好
rules - 根据需要添加额外插件
3. Create Prettier Configuration
3. 创建Prettier配置
Create in project root using the template from . This configuration:
.prettierrcassets/.prettierrc- Sets 2-space indentation
- Uses single quotes for strings
- Removes trailing commas
- Sets 100-character line width
- Uses Unix line endings
Adjust formatting rules to match team style guide.
Create using to exclude:
.prettierignoreassets/.prettierignore- Build directories (.next, dist, out)
- Dependencies (node_modules, package-lock.json)
- Generated files
- Public assets
在项目根目录创建,使用中的模板。该配置:
.prettierrcassets/.prettierrc- 设置2空格缩进
- 字符串使用单引号
- 移除尾随逗号
- 设置100字符行宽
- 使用Unix换行符
根据团队风格指南调整格式化规则。
使用创建文件,排除以下内容:
assets/.prettierignore.prettierignore- 构建目录(.next、dist、out)
- 依赖包(node_modules、package-lock.json)
- 生成文件
- 公共资源
4. Set Up Husky and Lint-Staged
4. 配置Husky与lint-staged
Initialize Husky for git hooks:
bash
npx husky initThis creates directory with a hook.
.husky/pre-commitReplace the pre-commit hook content with:
bash
npx lint-stagedAdd lint-staged configuration to using the example from :
package.jsonreferences/package-json-config.mdjson
{
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"eslint --fix",
"prettier --write"
],
"*.{json,md,yml,yaml}": [
"prettier --write"
]
}
}This runs ESLint and Prettier on staged files before each commit.
初始化Husky以使用Git钩子:
bash
npx husky init此命令会创建目录,并生成钩子。
.husky/pre-commit将pre-commit钩子内容替换为:
bash
npx lint-staged在中添加lint-staged配置,可参考中的示例:
package.jsonreferences/package-json-config.mdjson
{
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"eslint --fix",
"prettier --write"
],
"*.{json,md,yml,yaml}": [
"prettier --write"
]
}
}此配置会在每次提交前对暂存文件运行ESLint和Prettier。
5. Add Package Scripts
5. 添加包脚本
Add the following scripts to (see for complete example):
package.jsonreferences/package-json-config.mdjson
{
"scripts": {
"lint": "eslint . --max-warnings 0",
"lint:fix": "eslint . --fix",
"format": "prettier --write .",
"format:check": "prettier --check .",
"prepare": "husky"
}
}These scripts enable:
- - Check for linting errors (fails on warnings)
npm run lint - - Auto-fix linting issues
npm run lint:fix - - Format all files with Prettier
npm run format - - Check formatting without modifying files
npm run format:check - - Automatically set up Husky when installing dependencies
prepare
在中添加以下脚本(完整示例见):
package.jsonreferences/package-json-config.mdjson
{
"scripts": {
"lint": "eslint . --max-warnings 0",
"lint:fix": "eslint . --fix",
"format": "prettier --write .",
"format:check": "prettier --check .",
"prepare": "husky"
}
}这些脚本的作用:
- - 检查代码错误(存在警告则执行失败)
npm run lint - - 自动修复代码检查问题
npm run lint:fix - - 使用Prettier格式化所有文件
npm run format - - 检查文件格式但不修改
npm run format:check - - 安装依赖包时自动配置Husky
prepare
6. Create GitHub Actions Lint Workflow
6. 创建GitHub Actions Lint工作流
Create using the template from . This workflow:
.github/workflows/lint.ymlassets/github-workflows-lint.yml- Runs on pull requests and pushes to main/master
- Checks out code and sets up Node.js
- Installs dependencies
- Runs both linting and format checks
- Fails CI if any issues are found
Customize the workflow:
- Adjust Node.js version as needed
- Modify branch names to match repository
- Add caching for faster CI runs
使用中的模板创建。该工作流:
assets/github-workflows-lint.yml.github/workflows/lint.yml- 在拉取请求和推送到main/master分支时运行
- 拉取代码并设置Node.js环境
- 安装依赖包
- 运行代码检查和格式检查
- 若存在问题则CI执行失败
自定义工作流:
- 根据需要调整Node.js版本
- 修改分支名称以匹配仓库设置
- 添加缓存以加速CI运行
7. Verify Setup
7. 验证配置
Test the complete setup:
- Lint check: Run to verify ESLint configuration
npm run lint - Format check: Run to verify Prettier configuration
npm run format:check - Pre-commit hook: Make a change and commit to test Husky and lint-staged
- CI workflow: Push to a branch and open a PR to verify GitHub Actions
Fix any configuration issues:
- Review ESLint errors and adjust rules if needed
- Format all files:
npm run format - Commit the configuration changes
测试完整配置:
- 代码检查:运行验证ESLint配置
npm run lint - 格式检查:运行验证Prettier配置
npm run format:check - 提交前钩子:修改代码并提交,测试Husky和lint-staged
- CI工作流:推送到分支并打开拉取请求,验证GitHub Actions
修复配置问题:
- 查看ESLint错误,必要时调整规则
- 格式化所有文件:
npm run format - 提交配置变更
8. Team Documentation
8. 团队文档
Document the setup for team members (see for template):
references/team-documentation.md- Explain the purpose of each tool
- Provide setup instructions for new developers
- Document how to temporarily bypass hooks (for emergencies only)
- Include troubleshooting steps for common issues
为团队成员编写配置文档(模板见):
references/team-documentation.md- 说明每个工具的用途
- 为新开发者提供设置指南
- 记录紧急情况下临时绕过钩子的方法
- 包含常见问题的排查步骤
Configuration Customization
配置自定义
ESLint Rules
ESLint规则
Adjust rule severity in :
eslint.config.mjs- - Disable rule
"off" - - Warning (doesn't fail CI)
"warn" - - Error (fails CI)
"error"
Common customizations:
- Disable specific rules:
'react/prop-types': 'off' - Adjust rule options:
'max-len': ['error', { code: 120 }] - Add project-specific rules
在中调整规则的严重程度:
eslint.config.mjs- - 禁用规则
"off" - - 警告(不导致CI失败)
"warn" - - 错误(导致CI失败)
"error"
常见自定义项:
- 禁用特定规则:
'react/prop-types': 'off' - 调整规则选项:
'max-len': ['error', { code: 120 }] - 添加项目特定规则
Prettier Options
Prettier选项
Modify formatting in :
.prettierrc- - Line length limit
printWidth - - Spaces per indentation level
tabWidth - - Semicolon preference
semi - - Quote style
singleQuote - - Trailing comma rules
trailingComma
在中修改格式化设置:
.prettierrc- - 行宽限制
printWidth - - 缩进空格数
tabWidth - - 分号偏好
semi - - 引号风格
singleQuote - - 尾随逗号规则
trailingComma
Lint-Staged Configuration
lint-staged配置
Customize pre-commit checks in :
package.json- Add file type patterns
- Include additional commands (tests, type checking)
- Adjust which files trigger which linters
Example with type checking:
json
{
"lint-staged": {
"*.{ts,tsx}": [
"eslint --fix",
"prettier --write",
"tsc-files --noEmit"
]
}
}在中自定义提交前检查:
package.json- 添加文件类型匹配模式
- 包含额外命令(测试、类型检查)
- 调整触发检查的文件与对应工具
带类型检查的示例:
json
{
"lint-staged": {
"*.{ts,tsx}": [
"eslint --fix",
"prettier --write",
"tsc-files --noEmit"
]
}
}Troubleshooting
故障排查
ESLint errors on existing code: Run then to auto-fix most issues.
npm run lint:fixnpm run formatHusky hooks not running: Ensure was run after Husky initialization. Check is executable.
npm install.husky/pre-commitCI failing but local passes: Verify Node.js version matches between local and CI. Check that all dependencies are in .
package.jsonConflicts between ESLint and Prettier: Ensure is last in extends array to disable conflicting ESLint formatting rules.
eslint-config-prettier现有代码出现ESLint错误:运行,再执行以自动修复大部分问题。
npm run lint:fixnpm run formatHusky钩子未运行:确保初始化Husky后执行了。检查文件是否可执行。
npm install.husky/pre-commit本地通过但CI失败:验证本地与CI环境的Node.js版本是否一致。检查所有依赖包是否已添加到。
package.jsonESLint与Prettier冲突:确保在extends数组的最后一位,以禁用冲突的ESLint格式化规则。
eslint-config-prettierResources
资源
scripts/
scripts/
No executable scripts needed for this skill.
本技能无需可执行脚本。
references/
references/
- - Complete package.json example with all scripts and lint-staged configuration
package-json-config.md - - Template for documenting the setup for team members
team-documentation.md
- - 包含所有脚本和lint-staged配置的完整package.json示例
package-json-config.md - - 为团队成员编写配置文档的模板
team-documentation.md
assets/
assets/
- - ESLint v9 flat config template with React, TypeScript, and Next.js support
eslint.config.mjs - - Prettier configuration with recommended settings
.prettierrc - - Files and directories to exclude from formatting
.prettierignore - - GitHub Actions workflow for automated lint checks
github-workflows-lint.yml
- - 支持React、TypeScript和Next.js的ESLint v9扁平配置模板
eslint.config.mjs - - 推荐的Prettier配置
.prettierrc - - 需排除格式化的文件和目录
.prettierignore - - 用于自动代码检查的GitHub Actions工作流模板
github-workflows-lint.yml