@path 导入语法
使用 @./standards/coding-style.md 引用实现模块化。
@path 导入语法 是 CoddyKit 上的免费 Claude Architect 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Claude Architect 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Claude Architect 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Modularize CLAUDE.md
A single, ever-growing CLAUDE.md becomes hard to read and burns context tokens on every turn. The @path import syntax lets you split rules into focused files and pull them in by reference.
Instead of pasting 400 lines of coding standards directly, you write one line that imports them. The result: a lean top-level file that stays readable while the details live in dedicated, reusable modules.
The Basic Import
An import is just a line beginning with @ followed by a path. When Claude Code loads your CLAUDE.md, it inlines the referenced file's contents at that point.
Use a relative path from the file doing the importing. The example below pulls a shared coding-style module into the project memory.
# CLAUDE.md
## Project Standards
@./standards/coding-style.md
@./standards/testing.mdRelative vs Absolute Paths
Paths are resolved relative to the file that contains the import. @./standards/coding-style.md looks for a standards/ folder next to your CLAUDE.md.
@./standards/coding-style.md— sibling subfolder (most common, VCS-friendly)@../shared/rules.md— parent directory- Keep imports inside the repo so teammates who clone the project get the same modules.
A Clean Modular Layout
Group related rules into small files under a standards/ directory, then import each one. Each module has a single responsibility, so you can update testing rules without touching style rules.
This mirrors good code design: small, named, composable units beat one giant file.
.
├── CLAUDE.md # imports the modules below
└── standards/
├── coding-style.md
├── testing.md
└── git-workflow.mdImports Respect the Hierarchy
CLAUDE.md has layers: user-level ~/.claude/CLAUDE.md (personal, NOT shared via VCS), project-level ./CLAUDE.md (shared via VCS), and directory-level files scoped to a subtree.
@path imports work within any of these. Because a user-level file is not shared, new teammates miss it — so put rules that the whole team needs in a project-level file (and its imports), not in your personal one.
Shared, Not Personal
The whole point of importing @./standards/coding-style.md from a project CLAUDE.md is that the path and the file both live in version control. Clone the repo, and the standards come with it.
Contrast that with a rule buried in ~/.claude/CLAUDE.md: it only exists on your machine. A teammate's Claude session would never see it. Modular project imports keep guidance consistent across the team.
# Shared with the team (committed):
# ./CLAUDE.md -> @./standards/coding-style.md
# Personal only (NOT committed, teammates miss it):
# ~/.claude/CLAUDE.mdImports Always Load
Anything you @import from CLAUDE.md is part of the context on every turn the file is active. That is great for rules that always apply — naming conventions, commit format, review expectations.
But it also means every imported line costs tokens continuously. Import what is always relevant; for rules that only matter sometimes, there is a better tool (next scene).
When @import Is the Wrong Tool
If a rule only applies while editing certain files, don't bloat the always-loaded context with it. Use .claude/rules/ files with YAML frontmatter instead.
A paths glob makes the rule load only when you edit a matching file — saving context and tokens versus a monolithic always-on CLAUDE.md.
---
paths:
- "src/**/*.tsx"
---
# React component rules
Use function components and hooks; no class components.Choosing Between Them
Two complementary mechanisms:
- @path imports in CLAUDE.md — for rules that should always be in context (project-wide standards). Modularizes the file; everything still loads every turn.
- .claude/rules/ with paths frontmatter — for rules that should load conditionally, only when editing matching files. Saves tokens.
An architect picks based on whether the guidance is universal or file-scoped.
Composing Modules
You can import several modules from one CLAUDE.md, mixing concerns cleanly. Keep the top file as an index that explains structure, and let each import carry the detail.
Because imports are plain references, you can reorder, add, or remove a module without rewriting the rest — exactly the maintainability win modular config is meant to deliver.
# CLAUDE.md
This project's rules are modular. See:
@./standards/coding-style.md
@./standards/testing.md
@./standards/git-workflow.md
@./standards/security.mdEditing and Persisting
You don't have to hand-edit memory files. The /memory command edits CLAUDE.md, and those changes persist across sessions — handy for adding a new @import line as your standards grow.
Keep the imported modules themselves under version control so the persisted rules and their references stay in sync for everyone on the team.
# In Claude Code:
/memory
# Add to CLAUDE.md, then save:
@./standards/coding-style.mdQuick Check
Apply what you've learned about @path imports and the CLAUDE.md hierarchy.
Recap
Key takeaways:
- @path imports (e.g.
@./standards/coding-style.md) modularize CLAUDE.md; paths resolve relative to the importing file. - Imported content loads on every turn — use it for universal, always-relevant rules.
- Put shared standards in a project-level CLAUDE.md (shared via VCS), not user-level (~/.claude/, personal and missed by teammates).
- For rules that apply only to certain files, prefer
.claude/rules/with apathsfrontmatter glob to save context and tokens. /memoryedits CLAUDE.md and persists changes across sessions.
常见问题解答
「@path 导入语法」课时是免费的吗?
是的 — 「@path 导入语法」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Claude Architect 课程的其余内容,请升级到 CoddyKit PRO。 Claude Architect 课程共包含 4 节课。
「@path 导入语法」这节课中我会学到什么?
使用 @./standards/coding-style.md 引用实现模块化。 你通过在浏览器中直接运行的动手代码来练习 Claude Architect,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Claude Architect 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Claude Architect 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「@path 导入语法」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Claude Architect 课中编写并运行代码吗?
能。每节 Claude Architect 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。