Advertisement
Advanced TypeScript, Modules

How does 'module resolution' (the 'moduleResolution' compiler option) affect how TypeScript locates the right .d.ts file for a bare package import?

Short answer

moduleResolution controls the ALGORITHM TypeScript uses to search for a module's files/types given an import specifier (e.g.

Save TypeScript mock interview
Advertisement
AModel answer
moduleResolution controls the ALGORITHM TypeScript uses to search for a module's files/types given an import specifier (e.g. 'classic' vs 'node' vs 'bundler' strategies) — different settings walk node_modules, package.json 'exports' maps, or file-extension resolution differently, and mismatches here are a common source of 'Cannot find module' errors despite the package clearly being installed.
// tsconfig.json
{
  "compilerOptions": {
    "moduleResolution": "bundler" // matches modern bundler resolution behavior closely
  }
}
Real-world example
Fixing a 'Cannot find module' error that only appears in TypeScript despite the import working fine at runtime via the bundler.
Likely follow-ups
  • Why was the 'bundler' moduleResolution setting introduced as an alternative to 'node' / 'node16'?
Related topics