设置 CLAUDE.md 文件
阅读时间:5 分钟快速教程
配置项目特定的设置和上下文,以最大化 Claude Code 对您特定项目的有效性。
什么是 CLAUDE.md?
CLAUDE.md 是一个特殊的配置文件,允许您为 Claude Code 提供持久的上下文和项目特定的设置。它帮助 Claude 更好地理解您的项目并提供更准确的帮助。
主要优势
- 跨会话的持久项目上下文
- 针对项目特定需求的自定义配置
- 改进的代码建议
- 更好地理解项目架构
- 减少重复解释的需要
创建基本的 CLAUDE.md 文件
让我们从一个基本的 CLAUDE.md 文件结构开始:
步骤 1:创建文件
在项目根目录中创建一个名为 CLAUDE.md
的文件:
# Project: My Awesome App ## Project Description A brief description of what your project does and its main features. ## Tech Stack - Frontend: React, TypeScript - Backend: Node.js, Express - Database: MongoDB - Testing: Jest, React Testing Library ## Code Conventions - We use Prettier for formatting - ESLint for linting - Functional components with hooks for React - 2-space indentation - camelCase for variables and functions - PascalCase for components and classes ## Project Structure - /src - Main source code - /components - React components - /pages - Page components - /api - API routes - /utils - Utility functions - /hooks - Custom React hooks - /styles - CSS/SCSS files - /public - Static assets - /tests - Test files
步骤 2:添加上下文特定的部分
使用更具体的上下文来增强您的 CLAUDE.md:
## Important Notes - User authentication uses JWT stored in HttpOnly cookies - API calls should use the client utility in /src/utils/api.ts - New components should have a companion test file - State management uses React Context + useReducer ## Known Issues - Performance issues with large data sets in the dashboard component - Mobile menu animation is choppy on older iOS devices ## Future Plans - Implementing real-time updates with WebSockets - Adding a dark mode theme - Migrating to Next.js for improved SEO
高级配置
通过这些高级功能将您的 CLAUDE.md 文件提升到新的水平:
自定义指令
添加关于 Claude 在处理您的项目时应该如何表现的具体指令:
## Instructions for Claude - Always suggest TypeScript types for new functions - Prioritize performance optimizations - Use React Query for data fetching - Follow the existing error handling pattern - Include JSDoc comments for public functions - Prefer functional programming approaches
环境配置
提供有关开发环境的信息:
## Environment Setup - Node.js v16+ - npm scripts: - `npm start` - Start development server - `npm test` - Run tests - `npm run build` - Build for production - `npm run lint` - Run linter - Environment variables (examples, not actual values): - API_URL=http://localhost:3001 - DEBUG=false - FEATURE_FLAGS_ENABLED=true
架构图
包含 Mermaid 图表以可视化架构:
## Architecture ```mermaid graph TD A[Client] --> B[API Gateway] B --> C[Auth Service] B --> D[User Service] B --> E[Content Service] C --> F[(Auth DB)] D --> G[(User DB)] E --> H[(Content DB)] ``` ## Component Relationships ```mermaid graph TD A[App] --> B[Layout] B --> C[Header] B --> D[Main Content] B --> E[Footer] D --> F[Dashboard] F --> G[UserStats] F --> H[ActivityFeed] F --> I[Recommendations] ```
最佳实践
遵循这些最佳实践来维护有效的 CLAUDE.md 文件:
保持更新
- 每当项目架构发生变化时更新 CLAUDE.md
- 随着项目复杂性的增长添加新的部分
- 删除过时的信息
- 将其视为活文档
使用多个 CLAUDE.md 文件
对于较大的项目,考虑在不同的目录中使用多个 CLAUDE.md 文件:
/CLAUDE.md
- 根项目上下文/frontend/CLAUDE.md
- 前端特定细节/backend/CLAUDE.md
- 后端特定细节/docs/CLAUDE.md
- 文档指南
版本控制
- 将 CLAUDE.md 提交到您的代码仓库
- 在代码审查期间审查 CLAUDE.md 的更改
- 考虑将 CLAUDE.md 更新添加到您的完成定义中
相关教程
探索这些相关教程以增强您的 Claude Code 工作流程:
- 学习如何有效使用 /clear 命令进行上下文管理
- 掌握 Claude Code 的Git 集成最佳实践
- 查看高级上下文管理技术