{"success":true,"name":"keep-module-dependencies-flowing-one-way-break-import-cycles","description":"Two modules import each other, or utils/services/models tangle into loops","category":"ai-brimind","path":"ai-brimind/keep-module-dependencies-flowing-one-way-break-import-cycles/SKILL.md","file":"SKILL.md","content":"---\nname: keep-module-dependencies-flowing-one-way-break-import-cycles\ndescription: \"Two modules import each other, or utils/services/models tangle into loops\"\n---\n\n# Keep Module Dependencies Flowing One Way: Break Import Cycles Before They Break You\n\n## When to use\nModules in the codebase import each other in circles (A needs B, B needs A) — or you are about to add the import that closes such a loop — producing undefined imports or untouchable code.\n\n## Example request\nmemberService imports clubService for club checks, clubService imports memberService for member counts → Node resolves the cycle by handing one of them an incomplete module; the symptom is 'X is not a function' or undefined at require time, appearing only in certain entry orders. A constants file that imports a service 'just for one helper' → the leaf of the dependency tree now points back at the trunk; leaves (constants, pure utils) must import NOTHING from upper layers, that is what makes them safe to import from everywhere. Two services needing each other's logic → the shared piece wants to move DOWN into a third module both import; extracting the common dependency is the standard cycle-breaker. A one-file script with no imports → nothing to architect; this discipline is for codebases where modules have relationships.\n\n## Instructions\nAct as a city planner for your codebase who enforces one traffic rule: dependencies flow in one direction — from specific down to general, from routes → services → models/utils/constants — because a cycle (A imports B imports A) is not a style problem, it is a loading-order bomb: the module system must hand one side an unfinished copy of the other, and the resulting 'undefined is not a function' appears or vanishes depending on which file happened to load first, which is the most disorienting bug class a junior can meet. Do this: (1) Hold the layer map in your head and say it when placing code: constants and pure utils at the bottom (import nothing above them), models above those, services above models, routes/components at the top — every import statement should point DOWNWARD on this map; an upward import is the smell, even before it closes a cycle. (2) When two same-level modules need each other, diagnose what the cycle is telling you — it is always one of two things: a shared piece is trapped in the wrong place (extract it DOWN into a module both can import: the shared type, the common calculation, the constant — your shared-vocabulary discipline is a special case of exactly this), or the caller should pass the data in rather than the module fetching it (a function that receives the club as a parameter needs no clubService import — dependency passed as argument beats dependency imported, and it makes the function testable for free). (3) Never 'fix' a cycle with the lazy tricks that hide it — moving the require() inside the function body or reordering imports until the error stops — those defuse today's symptom while leaving the loop for the next entry point to trip; the structure is the bug, restructure it. (4) When the runtime hands you the classic symptoms (undefined import, 'cannot access before initialization', behavior differing between npm run dev and a script entry), suspect a cycle FIRST and trace it: start from the failing import and follow the chain until it revisits a file — reading five import lines beats three hours of console.logging a ghost. (5) Verify direction on every new file: before writing its imports, place it on the layer map and check each import points down. Only call the module structure sound when you can draw the dependency arrows for your core folders and none of them form a loop — a codebase where everything imports everything is not flexible, it is one refactor away from nothing loading at all.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #241\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}