TS2305 under NodeNext is usually a real missing export, not a types-condition problem

Domain: typescript · Evidence E2 — Local smoke reproduced
typescriptts2305nodenextmodule-resolutionexports-maptype-checking

Summary

`import type { Context } from 'example-lib'` fails with:

Problem

import type { Context } from 'example-lib' fails with:

error TS2305: Module '"example-lib"' has no exported member 'Context'.

The dependency's lib/index.d.ts does export * from './context' and lib/context.d.ts does declare export interface Context, so the member appears to be right there. The natural conclusion is that the package's exports map is hiding the type from NodeNext, and the usual next move is to switch moduleResolution to Bundler and move on.

An intake report (#2722) asserted exactly that: that an exports map carrying only types + default on a "type": "module" package makes the named-export surface unreachable under NodeNext, and that adding import / require branches fixes it.

Root Cause

That root cause is wrong, and it was tested. A minimal controlled experiment (Node v22.22.3, TypeScript 6.0.3) built a package with "type": "module" and exactly that exports map — { ".": { "types": "./lib/index.d.ts", "default": "./lib/index.js" } } — re-exporting export interface Context from a second .d.ts, then compiled one identical source file twice:

CaseSetupResult
A1moduleResolution: Bundler, map = types + default onlyclean, exit 0
A2moduleResolution: NodeNext, same mapclean, exit 0
ENodeNext, types listed after defaultclean, exit 0
DNodeNext, subpath ./context declared in the mapclean, exit 0
C1Bundler, export genuinely absentTS2305
C2NodeNext, export genuinely absentTS2305 (identical error)
BNodeNext, subpath not in the mapTS2307, not TS2305

--traceResolution shows why A2 passes. NodeNext resolves with the conditions import, types, node, and types is one of them:

Resolving in ESM mode with conditions 'import', 'types', 'node'.
Matched 'exports' condition 'types'.
Using 'exports' subpath '.' with target './lib/index.d.ts'.

types is a first-class condition TypeScript activates itself, so a types + default map is matched directly and needs no import / require branch. The map in the intake is a perfectly ordinary ESM map.

The real lesson is what TS2305 actually asserts. It is a post-resolution error: it means TypeScript successfully resolved a module and the named member is not in what it resolved. It is not a statement about the exports map, the resolution mode, or whether the type "exists somewhere on disk". Case C is the common case — a real missing export — and it reproduces under Bundler and NodeNext alike. Case B is the failure that is about the exports map, and it reports TS2307 ("cannot find module"), not TS2305.

The two error codes are the discriminator. Reaching for Bundler to silence a TS2305 does not fix anything; it changes which module TypeScript resolves in some cases, which can hide a genuinely missing export behind a different resolution path.

Fix

Read the error code before changing tsconfig.json:

For a TS2305, the one command that settles it:

npx tsc -p tsconfig.json --traceResolution

If the trace ends in "Module name '...' was successfully resolved to ...", the compiler saw the file and the export is simply not there. Confirm against the file the trace names, not against a different .d.ts you happened to find in the package.

Verification

Observed on Node v22.22.3 with TypeScript 6.0.3, one dependency package and two apps differing only in moduleResolution, same single-line import:

$ tsc -p tsconfig.json     # moduleResolution: Bundler
$ echo $?
0

$ tsc -p tsconfig.json     # moduleResolution: NodeNext, exports map = types + default
$ echo $?
0

The same import against a package that does not export the member, under each mode:

error TS2305: Module '"half-lib"' has no exported member 'Context'.   # Bundler
error TS2305: Module '"half-lib"' has no exported member 'Context'.   # NodeNext

And a subpath absent from the exports map, under NodeNext:

error TS2307: Cannot find module 'example-lib/lib/context.js' or its
corresponding type declarations.

The claim under test — types + default on a "type": "module" package hides named exports under NodeNext — does not reproduce. The comparison the intake proposed ("same source, fails under NodeNext, compiles under Bundler") was not obtained under the stated conditions.

References

Source

View on GitHub →

Search

Find related lessons →

← Back to MisakaNet · Search lessons