Source-root imports
A project wants short, non-relative imports rooted at its source directory.
TypeScript · Const
Sets the directory TypeScript uses to resolve bare module specifiers. It can make imports such as "utils/date" resolve from a configured project directory before node_modules.
Set baseUrl in compilerOptions to establish a directory for resolving bare module specifiers such as "utils/date". TypeScript checks that directory before node_modules, but baseUrl affects compiler resolution only; the runtime or bundler must independently understand the same import mapping.
{"compilerOptions":{"baseUrl":"./src"}}| Name | Type | Description |
|---|---|---|
baseUrl | string | Directory used as the starting point for resolving bare module specifiers; relative values are based on tsconfig.json. |
No runtime value; this tsconfig compiler option changes TypeScript's module-resolution behavior.
// tsconfig.json
// {
// "compilerOptions": { "baseUrl": "./src" }
// }
import { formatDate } from "utils/date";
console.log(formatDate(new Date()));With baseUrl set to ./src, the bare import resolves from the project's src directory.
A project wants short, non-relative imports rooted at its source directory.
An AMD browser application loads modules from one deployment directory.
A project uses paths mappings whose targets should be relative to a shared base directory.
pathsmoduleResolutionrootDirs