Full answer
Folders by file type (components/, services/, pipes/) work for small apps. At scale a change to one feature touches five folders, and anything can import anything.
A layout that scales:
apps/shell/ app config, routes
libs/
courses/
feature-list/ smart components, routes
feature-detail/
data-access/ services, state, API calls
ui/ presentational components
util/ pure functions
shared/
ui/ design system
util/
Rules (based on the Nx guidance):
- Group by domain first (courses, billing), then by type of library (feature, data-access, ui, util).
- Allowed direction: feature can use data-access, ui and util. ui and util cannot use data-access or feature. data-access can use util. A domain should not reach into another domain's internals.
- Each library has one public API file (
index.ts). Others import only from there, so internals can change freely. Do not export everything. An export is a promise. - Lazy load features by route.
- Avoid cycles. Move shared code down a layer, or use an event or a contract.
- Keep shared small. A giant
shared/folder becomes a second monolith.
Enforcing rules: write them in tooling. In an Nx workspace use @nx/enforce-module-boundaries with tags; without Nx, use ESLint no-restricted-imports, eslint-plugin-boundaries, or dependency-cruiser. Run it in CI so a violating import fails the build. A small script can also check the rule, as in the sample.
Process around the structure: code owners per library (CODEOWNERS), generators for new libraries and components, affected-only builds and tests (Nx cache) so CI stays fast, and architecture decision records for rules.
Honest trade-off: more libraries mean more configuration. Start with folders and boundaries inside one app, and split into libraries when team size or build time requires it.
// file: check-boundaries.ts
type Layer = 'feature' | 'data-access' | 'ui' | 'util';
const ALLOWED: Record<Layer, Layer[]> = {
feature: ['data-access', 'ui', 'util'],
'data-access': ['util'],
ui: ['util'],
util: [],
};
interface SourceFile {
path: string;
content: string;
}
function parse(path: string): { domain: string; layer: Layer; internal: boolean } | null {
const match = path.match(/libs\/([^/]+)\/(feature|data-access|ui|util)[^/]*\/(.*)$/);
if (!match) {
return null;
}
return { domain: match[1], layer: match[2] as Layer, internal: match[3] !== 'index.ts' };
}
export function check(files: SourceFile[]): string[] {
const problems: string[] = [];
for (const file of files) {
const from = parse(file.path);
if (!from) {
continue;
}
for (const m of file.content.matchAll(/from '@school\/([^/']+)\/([^/']+)(\/[^']*)?'/g)) {
const [, domain, layerName, deepPath] = m;
const layer = layerName.replace(/-.*/, '') as Layer;
const target = layerName.startsWith('data-access') ? 'data-access' : layer;
if (deepPath) {
problems.push(`${file.path}: deep import of ${m[0].slice(5)}, use the public API`);
}
if (domain !== from.domain && target !== 'ui' && target !== 'util') {
problems.push(`${file.path}: ${from.domain} must not use ${domain}/${target}`);
}
if (domain === from.domain && !ALLOWED[from.layer].includes(target as Layer) && target !== from.layer) {
problems.push(`${file.path}: ${from.layer} must not use ${target}`);
}
}
}
return problems;
}
const sample: SourceFile[] = [
{ path: 'libs/courses/feature-list/list.ts', content: `import { Api } from '@school/courses/data-access';` },
{ path: 'libs/courses/ui/card.ts', content: `import { Api } from '@school/courses/data-access';` },
{ path: 'libs/billing/feature-pay/pay.ts', content: `import { Course } from '@school/courses/feature-list';` },
{ path: 'libs/billing/feature-pay/pay2.ts', content: `import { x } from '@school/courses/data-access/internal/store';` },
];
const problems = check(sample);
console.log(problems.length === 0 ? 'No boundary problems' : problems.join('\n'));Angular 20.3. The script is plain TypeScript run with Node 24 (type stripping) twice with identical output. It checks sample files held in memory, so it needs no project. In a real repository use ESLint or dependency-cruiser.
Key points
- Group by domain, then by type: feature, data-access, ui, util.
- Define who may import whom, and expose a public API per library.
- Enforce with lint or a dependency checker in CI.
- Keep shared small, avoid cycles, and add structure only as the team grows.
Common mistakes
- A large shared folder that everything depends on.
- Importing deep internal paths of another library instead of its public API.
- Rules written in a wiki and not enforced by tools.
Follow-ups
- How would you break a circular dependency between two features?
- What belongs in `data-access` and what in `util`?
- How do you keep CI fast in a large monorepo?
Tips
- Show the layers as a small picture and state the direction of imports.
- Say that rules must be automated.
- Mention the cost of too many small libraries.
Evaluation rubric
- Gives a clear structure with reasons.
- Defines import rules and a public API.
- Automates enforcement and discusses trade-offs.