Sub topic 10
`paths` Aliases and Their Limits
This chapter assumes you can write TypeScript imports and exports, read tsconfig.json, and run tsc. It also assumes you know that Node.js supports both CommonJS and ES modules.
A paths alias gives TypeScript another name for a file or directory. It can replace a long relative import such as:
import { formatUser } from "../../../shared/formatUser.js";with:
import { formatUser } from "@/shared/formatUser.js";The alias is a compiler lookup rule. It is not automatically a runtime lookup rule.
Configure an alias
A typical application configuration looks like this:
{
"compilerOptions": {
"module": "preserve",
"moduleResolution": "bundler",
"target": "es2022",
"strict": true,
"noEmit": true,
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
}
},
"include": ["src"]
}With this configuration, @/shared/formatUser maps to src/shared/formatUser. The * is a wildcard. The text matched by the first * is inserted into the second pattern.
For example:
| Import | TypeScript target |
|---|---|
| `@/models/user` | `src/models/user` |
| `@/shared/formatUser` | `src/shared/formatUser` |
The baseUrl value makes the mapping relative to the project directory. Modern TypeScript can use paths without baseUrl, but setting it explicitly often makes the configuration easier to read.
The emitted-import limit
TypeScript usually does not rewrite an alias in the generated JavaScript. Consider this source:
import { greet } from "@/greet.js";
console.log(greet("world"));A compiler or bundler may type-check this successfully, but emitted JavaScript can still contain:
import { greet } from "@/greet.js";Node.js does not understand @/greet.js by default. The program can therefore pass type checking and fail when it starts.
A bundler such as Vite, webpack, or esbuild can be configured with the same alias. The bundler then turns the alias into a real file path while building the application. Your editor, TypeScript, bundler, and test runner must agree on the mapping.
`paths` with `nodenext`
nodenext models Node.js module resolution. It is a good choice when Node.js runs the emitted files directly. A paths alias can still help TypeScript, but Node.js needs a separate runtime mechanism.
For a package-owned internal alias, Node's package.json imports field is often a better match:
{
"type": "module",
"imports": {
"#app/*": "./dist/*"
}
}Then source code can use:
import { greet } from "#app/greet.js";The # prefix identifies a package import. Node.js understands it at runtime, and TypeScript can use the package map when the selected resolution mode supports it. The source and output directory layout must match the map.
Do not confuse paths with package publishing. A published package's consumers cannot use your private paths configuration. They need valid package entry points and an exports map.
`paths` with `bundler`
bundler is usually the simpler choice for an application whose imports are processed by a bundler. The bundler can resolve aliases, extensionless imports, and other conventions that Node.js itself would reject.
You still need to configure the alias in every tool that resolves modules:
- TypeScript, using
compilerOptions.paths - The bundler, using its alias setting
- The test runner, if it resolves imports independently
- Runtime tools used outside the bundle
A common failure occurs when tests use a different resolver from the production build. The application builds successfully, but tests report that an aliased module cannot be found.
Choosing a strategy
Use paths for convenient type-checking and bundler aliases in an application. Use relative imports when direct Node.js execution and maximum portability matter. Use package imports for aliases that Node.js must understand inside one package. For a library, publish through exports rather than expecting consumers to copy your paths settings.
Exercise
A project compiles with a paths alias but Node.js reports that the module cannot be found. What is the likely cause, and what should you check?
Show answer
TypeScript understood the alias, but Node.js did not. Check the emitted import and configure the bundler, Node package imports, or another runtime resolver to use the same mapping.