typescript modules ai_generated true

error TS1259: Module can only be default-imported using the 'esModuleInterop' flag

ID: typescript/ts1259-can-only-default-import-esinterop

Also available as: JSON · Markdown
97%Fix Rate
98%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
5 active

Root Cause

Trying to default-import a CommonJS module without esModuleInterop enabled.

generic

Workarounds

  1. 97% success Enable esModuleInterop in tsconfig.json: 'esModuleInterop': true
    Allows default imports from CJS modules; recommended by TypeScript team

    Sources: https://www.typescriptlang.org/tsconfig#esModuleInterop

  2. 85% success Also enable allowSyntheticDefaultImports if esModuleInterop is not feasible
    Less strict; only changes type checking, not emit behavior

Dead Ends

Common approaches that don't work:

  1. Using require() instead of import 70% fail

    Bypasses TypeScript module checking; loses type information

  2. Using import * as X from 'module' 55% fail

    Works but makes all usage look like X.default; awkward API

Error Chain

Frequently confused with: