Sub topic 8
Path Mapping with `paths` and `baseUrl`
Prerequisites and the problem
Assume you know basic TypeScript types, imports, exports, tsc, and tsconfig.json.
A relative import names a file from the importing file’s location:
import { formatDate } from "../../shared/format-date.js";As a project grows, these paths become hard to read and easy to break. TypeScript path mapping lets you create aliases such as @/shared/format-date. An alias is an alternative name for a module.
The baseUrl option gives TypeScript a starting directory for non-relative imports. The paths option maps alias patterns to files below that directory.
Configure an alias
Suppose the project has this structure:
project/
src/
app.ts
shared/
format-date.ts
tsconfig.jsonA matching configuration is:
{
"compilerOptions": {
"target": "es2022",
"module": "nodenext",
"moduleResolution": "nodenext",
"strict": true,
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
},
"outDir": "dist"
},
"include": ["src"]
}The pattern @/* matches an import beginning with @/. TypeScript replaces that prefix with src/, relative to baseUrl.
The source file can then import the module like this:
// src/app.ts
import { formatDate } from "@/shared/format-date.js";
console.log(formatDate(new Date()));
// src/shared/format-date.ts
export function formatDate(value: Date): string {
return value.toISOString().slice(0, 10);
}The alias improves the source code, but it does not move any files.
How resolution works
When TypeScript sees @/shared/format-date.js, it checks the paths mappings. It applies the matching pattern and looks under the configured base directory. In this example, it finds src/shared/format-date.ts.
The mapping affects TypeScript’s type checking and module resolution. It usually does not rewrite the emitted JavaScript import. With the configuration above, the output may still contain:
import { formatDate } from "@/shared/format-date.js";Node.js does not automatically understand this alias. A bundler, runtime loader, or build step must resolve it too.
Common errors and choices
| Situation | Suitable choice |
|---|---|
| Importing a nearby file | Use a relative import |
| Importing from a stable project alias | Use `paths` and configure the runtime |
| Publishing a library | Prefer public package exports over private source aliases |
| Sharing aliases across tools | Keep TypeScript, the bundler, tests, and runtime consistent |
Do not use paths to pretend that a package is installed. If an import refers to a real external package, declare that package in dependencies or devDependencies.
A broad mapping can also hide mistakes. Prefer specific aliases when a project has several independent areas:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@app/*": ["src/app/*"],
"@shared/*": ["src/shared/*"]
}
}
}After changing tsconfig.json, restart editors and development servers if they cache configuration.
Exercise
Why can tsc accept @/shared/format-date.js while Node.js fails when running the emitted JavaScript?
Show answer
paths changes TypeScript's compile-time resolution, but it does not generally rewrite the emitted import. Node.js needs its own alias configuration or a different import path.