Sub topic 9
Importing features through path aliases
This chapter assumes you know TypeScript modules, the Angular CLI, and the barrel files from the previous chapter. It shows how path aliases give each feature a stable import name.
The problem with relative imports
Without aliases, code reaches into other features with long relative paths:
import { Order } from '../../../orders/data-access/order.model';This has three problems:
- Moving a file breaks every import that climbs out of it.
- The path points at an internal file, so it bypasses the feature's barrel.
- Readers cannot tell at a glance which feature the import belongs to.
A path alias maps a short name to a location. TypeScript and the Angular build both read the mapping from tsconfig.json.
Declaring aliases
Add a paths entry under compilerOptions:
{
"compilerOptions": {
"paths": {
"@app/orders": ["src/app/orders/index.ts"],
"@app/shared/*": ["src/app/shared/*"]
}
}
}There are two kinds of entry here:
@app/ordersis an exact alias. It points at one file, the feature's barrel.@app/shared/*is a wildcard alias. The*matches any sub-path, so@app/shared/ui/buttonresolves tosrc/app/shared/ui/button.
Depending on your setup, you may also need "baseUrl": "." so the paths resolve from the project root. Recent TypeScript versions can resolve paths relative to the tsconfig.json file without baseUrl.
Consumers now import through the barrel:
import { ORDERS_ROUTES } from '@app/orders';
import type { Order } from '@app/orders';If OrdersStore is not exported from index.ts, this import fails to compile. The alias and the barrel together make the feature's public API the only way in.
Using aliases with lazy routes
Aliases work inside loadChildren too:
import { Routes } from '@angular/router';
export const routes: Routes = [
{
path: 'orders',
loadChildren: () => import('@app/orders').then((m) => m.ORDERS_ROUTES),
},
];The dynamic import() still creates a separate chunk. The barrel is the entry point of that chunk.
Common errors
| Symptom | Likely cause |
|---|---|
| `Cannot find module '@app/orders'` in the editor | The editor uses a different `tsconfig` than the build. Check the `extends` chain and restart the TypeScript server. |
| Works in the editor, fails in unit tests | The test runner has its own resolver. Add a matching `moduleNameMapper` (Jest) or use a `tsconfig.spec.json` that extends the base config. |
| Aliases resolve, but internals are still imported directly | Aliases do not restrict anything. The `paths` entry only names a location. |
| Circular import warnings | The barrel re-exports a file that imports the same barrel. Import siblings with relative paths inside the feature. |
Choosing alias names
- Use one prefix for the whole app, such as
@app, so aliases are never confused with npm packages. - Create one exact alias per feature, pointing at its barrel.
- Use a wildcard only for areas meant to be imported by path, such as shared UI.
- Keep alias names equal to folder names so the name predicts the location.
Exercise
A teammate imports OrdersStore with import { OrdersStore } from '@app/orders', and the compiler reports that the module has no exported member OrdersStore. The store is defined in orders/data-access/orders.store.ts. What does this tell you, and what are two valid responses?
Show answer
The alias works, but the barrel orders/index.ts does not export OrdersStore, so it is not part of the feature's public API. One response is to export it from the barrel if other code legitimately needs it. The other is to keep it private and let the teammate use the feature's routes or an exported API instead of the store. Reaching into orders/data-access/ directly would bypass the boundary and should be avoided.