AI-friendly Node.js project architecture standards
Keep every file under 400 lines. Save 70-93% tokens. Get 3x more productive AI rounds.
AI agents working with large codebases hit the context window wall fast:
3000-line server.js โ ~40K tokens per read โ 20% of context gone
After 3-5 rounds โ context compression โ agent forgets everything
Split by function. Each file stays small. AI reads only what it needs:
200-line module โ ~2.7K tokens per read โ 1.3% of context
After 10-15 rounds โ still going strong โ no compression needed
| Scenario | Before | After | Savings |
|---|---|---|---|
| Read one game feature | 40K tokens | 2.7K tokens | 93% |
| Full dev round (readโeditโverify) | 52K tokens | 4K tokens | 92% |
| 4-round SDK session | 196K tokens | 69K tokens | 65% |
| Large data tool module | 84K tokens | 8K tokens | 90% |
โ
Single file โค 400 lines
โ
index.html โค 200 lines
โ
server.js (entry) โค 100 lines
โ
Tunable values โ config.json (hot-reloadable)
โ
Backend โ routes/ by domain + services/ for shared logic
โ
Frontend โ HTML skeleton + separate JS/CSS files
โ
Every project โ admin.html + routes/admin.js
game-name/
server.js โ Entry, routes only (<100 lines)
config.json โ Gameplay values (admin can edit live)
routes/
auth.js โ Login/user
game.js โ Game data API
social.js โ Leaderboard/sharing
admin.js โ Admin API + config CRUD
public/
index.html โ HTML skeleton (<200 lines)
admin.html โ Admin dashboard
css/style.css
js/
config.js โ Fetch config at startup
game.js โ Core game logic
renderer.js โ Rendering/visuals
ui.js โ UI interactions
audio.js โ Sound effects
assets/
project-name/
server.js โ Entry (<100 lines)
config.json โ API keys, schedules, thresholds
db.js โ DB init + helpers
routes/
api.js โ Query/export API
task.js โ Task management
admin.js โ Admin API
services/
crawler.js โ Business logic
scheduler.js โ Cron/intervals
public/
index.html โ HTML skeleton (<200 lines)
admin.html โ Admin dashboard
css/style.css
js/
app.js โ Main logic
table.js โ Table rendering
api.js โ Fetch wrapper
sdk-name/
src/ โ Source modules (<200 lines each)
core.js / auth.js / api.js / ui.js
sdk-name.js โ Built output (merged single file)
build.js โ Simple merge script
test/test.html โ Test page
CHANGELOG.md
Dev in src/ (small files for AI) โ build merges into single file for consumers.
// Load + serve (strip secrets)
const config = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
app.get('/api/config', (req, res) => {
const safe = { ...config };
delete safe.admin;
res.json(safe);
});
// Admin hot-reload (no restart needed)
app.post('/admin/config', requireAdmin, (req, res) => {
fs.writeFileSync('./config.json.bak', fs.readFileSync('./config.json'));
fs.writeFileSync('./config.json', JSON.stringify(req.body, null, 2));
Object.assign(config, req.body);
res.json({ ok: true });
});Every project gets admin.html:
- ๐ Password login (
x-admin-passwordheader) - โ๏ธ Visual config editor with save + hot-reload
- ๐ Stats overview (users / data / uptime)
- ๐ Config backup history + one-click restore
clawhub install nodejs-project-archgit clone https://github.com/abczsl520/nodejs-project-arch.git
cp -r nodejs-project-arch ~/.agents/skills/The skill auto-activates when you ask your AI to:
- "Create a new Node.js project"
- "Refactor this large codebase"
- "Split this file, it's too big"
Full docs on the Wiki:
| Page | Description |
|---|---|
| Game Architecture | H5 game standards + WeChat compatibility |
| Tool Architecture | Data tools, dashboards, API services |
| SDK Architecture | Libraries with build step |
| Config Pattern | Universal config.json pattern |
| Admin Dashboard | Admin panel standards |
| Token Savings | Detailed savings analysis |
| Skill | Purpose | Install |
|---|---|---|
| bug-audit | Dynamic bug hunting, 200+ pitfall patterns | clawhub install bug-audit |
| codex-review | Three-tier code review with adversarial testing | clawhub install codex-review |
| debug-methodology | Root-cause debugging, prevents patch-chaining | clawhub install debug-methodology |
| nodejs-project-arch (this) | AI-friendly architecture, 70-93% token savings | clawhub install nodejs-project-arch |
| game-quality-gates | 12 universal game dev quality checks | clawhub install game-quality-gates |
- OpenClaw โ AI agent framework
- ClawHub โ Skill marketplace
- Any AI coding agent (Claude, GPT, Codex, Gemini...)
MIT โ use it, fork it, improve it.
If this saves you tokens, give it a โญ