Sub topic 6
How TypeScript resolves an import to a file
This chapter assumes you know basic TypeScript types, JavaScript import and export syntax, tsc, tsconfig.json, and npm package basics. The key idea is that TypeScript must resolve every module specifier, such as "./math.js" or "date-fns", to a file before it can type-check your program.
The resolution process
A module specifier is the text between the quotes in an import:
import { add } from "./math.js";
import { format } from "date-fns";TypeScript uses the specifier and your moduleResolution setting to search for a matching source file or declaration file.
The search depends on the specifier:
- A relative specifier starts with
./or../. - An absolute specifier starts with
/, although this is uncommon in application code. - A bare specifier names a package, such as
"date-fns"or"my-library".
For a relative import, TypeScript starts beside the importing file. For a package import, it searches node_modules directories and follows the package's metadata.
Relative imports and extensions
With Node.js native ES modules, relative imports in the emitted JavaScript normally include a file extension. TypeScript therefore commonly uses this source code:
// src/main.ts
import { add } from "./math.js";
console.log(add(2, 3));
// src/math.ts
export function add(a: number, b: number): number {
return a + b;
}The import says .js because Node.js will load math.js after compilation. TypeScript understands that the .js specifier corresponds to the .ts source file during type-checking.
A common error is writing import { add } from "./math" in a Node.js ESM project. The compiler may reject the import, or the generated program may fail when Node.js runs it.
Choosing a resolution strategy
The moduleResolution option selects the rules TypeScript follows. The setting should match the environment that will load the emitted JavaScript.
| Setting | Typical environment | Important behavior |
|---|---|---|
| `nodenext` | Modern Node.js projects | Follows Node.js ESM and CommonJS rules |
| `node16` | Node.js projects with fixed module behavior | Uses Node.js package and extension rules |
| `bundler` | Vite, webpack, esbuild, and similar tools | Models bundler resolution and usually permits extensionless imports |
| `classic` | Legacy TypeScript projects | Older rules; avoid for new projects |
For a Node.js ESM project, use matching module and resolution settings:
{
"compilerOptions": {
"target": "es2022",
"module": "nodenext",
"moduleResolution": "nodenext",
"strict": true,
"outDir": "dist"
},
"include": ["src"]
}When module is nodenext, TypeScript can infer the matching resolution behavior, so explicitly writing moduleResolution is optional. Writing it can make the intent clearer.
Package imports and declaration files
For a bare package import, TypeScript checks the package's package.json. It may use fields such as types, typings, or conditional exports entries to find a .d.ts file.
If a package has this metadata:
{
"name": "my-library",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
}TypeScript uses dist/index.d.ts for type information, while the runtime uses dist/index.js. If the package has no declarations, TypeScript may report that it cannot find a declaration file.
Path aliases
baseUrl and paths can define aliases for the type checker:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@app/*": ["src/*"]
}
}
}An import such as @app/math can then resolve during compilation. However, paths does not rewrite imports in emitted JavaScript. Your runtime or bundler must understand the same alias.
Exercise
A Node.js ESM project imports ./config from src/main.ts, but Node.js cannot load the compiled program. What is the likely correction?
Show answer
Use ./config.js in the TypeScript import and use a Node-compatible setting such as "module": "nodenext".