Sub topic 5
Setting up path aliases
This chapter assumes you can read a tsconfig.json, and that you use Vite or webpack and have a feature-based folder layout like the one from the earlier chapters.
Why aliases help
As folders nest deeper, relative imports turn into chains of dots:
import { Button } from "../../../../shared/ui/Button";This line breaks whenever you move the file. It also hides which part of the app you are importing from. A path alias maps a short prefix to a folder, so the same import becomes:
import { Button } from "@/shared/ui/Button";The @ prefix always means src/, no matter where the importing file sits.
Two places to configure
An alias must be set up in two places, and they do different jobs.
| Tool | What it does with the alias |
|---|---|
| TypeScript (`tsconfig.json`) | Type checking and editor features such as go-to-definition |
| Bundler (Vite or webpack) | Resolving the import when the app is built or served |
TypeScript does not rewrite import paths in the output. If you configure only tsconfig.json, the editor is happy and the build fails.
Configure TypeScript
Add a paths entry to compilerOptions:
{
"compilerOptions": {
"paths": {
"@/*": ["./src/*"]
}
}
}The * on each side is a wildcard. @/features/cart resolves to ./src/features/cart. Since TypeScript 4.1, paths works without baseUrl, and the targets are resolved relative to the tsconfig.json file.
Configure the bundler
For Vite, add a matching resolve.alias entry:
// vite.config.ts
import { fileURLToPath, URL } from "node:url";
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
"@": fileURLToPath(new URL("./src", import.meta.url)),
},
},
});For webpack, the equivalent is resolve.alias with an absolute path from path.resolve(__dirname, "src").
Skip that stray block; the diagram below shows the flow.
Other tools that read imports
Your test runner, linter, and Storybook each resolve imports on their own. Vitest reuses the Vite config when the alias lives there. Jest needs a moduleNameMapper entry. ESLint import plugins need to be told about the alias too. After you add an alias, run the app, the tests, and the linter once to confirm all three work.
Common errors
- "Cannot find module '@/...'" in the editor only. The
tsconfig.jsonentry is missing, or the editor is reading a different config file. Restart the TypeScript server. - The editor is fine but the build fails. The bundler alias is missing or points at the wrong folder.
- Tests fail but the app runs. The test runner has no alias mapping.
- Mixing styles. Using
@/features/cart/apiin one file and../apiin another makes the codebase hard to scan.
A convention to adopt
Use relative imports inside a feature, such as ./components/CartList. Use the alias when importing across features or from shared. A reader can then tell at a glance whether an import stays inside a module or crosses a boundary. The later chapter on boundary enforcement relies on this: a lint rule can match the @/features/* pattern to find cross-feature imports.
Exercise
Your editor resolves @/shared/ui/Button correctly, but vite build reports that it cannot resolve the import. Which file do you most likely need to change, and why?
Show answer
Change vite.config.ts and add resolve.alias for @ pointing at the src folder. The paths entry in tsconfig.json only affects type checking and the editor. Vite does not read it, so it needs its own alias.