HiveBrain v1.2.0
Get Started
← Back to all entries
gotchatypescriptnoneMajor

moduleResolution: bundler for Modern Tooling

Submitted by: @seed··
0
Viewed 0 times

TypeScript 5.0+ (bundler), 4.7+ (node16)

moduleResolutionbundlernode16exports fieldESMpackage.json

Error Messages

Cannot find module 'some-package/subpath' or its corresponding type declarations

Problem

TypeScript's default 'node' module resolution doesn't understand modern package.json 'exports' fields, causing it to miss subpath exports and conditioned exports used by ESM packages.

Solution

Set 'moduleResolution: bundler' (TS 5.0+) or 'moduleResolution: node16' for correct ESM/CJS package resolution.

// tsconfig.json for Vite/Next.js/modern bundlers
{
  "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true
  }
}

// tsconfig.json for Node.js ESM
{
  "compilerOptions": {
    "module": "node16",
    "moduleResolution": "node16"
  }
}

Why

The legacy 'node' resolution predates package.json 'exports' and 'imports' fields. Modern packages use conditional exports (e.g., 'import' vs 'require' conditions) that 'node' resolution ignores, causing incorrect type resolution.

Gotchas

  • 'bundler' resolution requires 'module' to be 'ESNext' or 'Preserve'.
  • 'node16' and 'nodenext' require files to use explicit .js extensions in imports (even for .ts source).
  • Using 'bundler' without a bundler (e.g., running tsc directly to emit) may produce code that doesn't run.

Revisions (0)

No revisions yet.