設定 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 集成最佳实践
- 查看進階上下文管理技术