typescript module_error ai_generated true

TS2856: Import assertions are not allowed on statements that compile to CommonJS 'require' calls

ID: typescript/ts-import-assertion-error

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
5 active

Root Cause

Import assertions (assert { type: 'json' }) are only valid with ESM output. Switch module target or use import attributes.

generic

Workarounds

  1. 88% success Use import attributes (with keyword) instead of assertions (assert keyword)
    // TS 5.3+: Use 'with' instead of 'assert'
    import data from './data.json' with { type: 'json' };

    Sources: https://devblogs.microsoft.com/typescript/announcing-typescript-5-3/#import-attributes

  2. 85% success Set module to ESM-compatible target in tsconfig.json
    {
      "compilerOptions": {
        "module": "esnext",
        "moduleResolution": "bundler",
        "resolveJsonModule": true
      }
    }

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

Dead Ends

Common approaches that don't work:

  1. Remove import assertions and hope JSON imports work 60% fail

    Without assertions, JSON imports may fail or have wrong types depending on the runtime

Error Chain

Leads to:
Preceded by:
Frequently confused with: