How does module resolution work in TypeScript

· Category: TypeScript

Short answer

TypeScript follows a module resolution strategy to map import specifiers to actual files on disk, defaulting to the Node strategy for modern projects.

How it works

In Node resolution, relative imports (./foo) look for .ts, .tsx, .d.ts, and then .js files. Non-relative imports search node_modules for a matching package and its types or main fields. The moduleResolution option controls the algorithm: node (or node10), node16, nodenext, and bundler.

Example

import { helper } from './utils';
// TypeScript checks utils.ts, utils.tsx, utils.d.ts, then utils/index.*

Why it matters

Understanding resolution helps debug Cannot find module errors. Path mapping via baseUrl and paths in tsconfig.json allows clean aliases like @/components without bundler configuration.