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
86%Fix Rate
88%Confidence
50Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 5 | active | — | — | — |
Root Cause
Import assertions (assert { type: 'json' }) are only valid with ESM output. Switch module target or use import attributes.
genericWorkarounds
-
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
-
85% success Set module to ESM-compatible target in tsconfig.json
{ "compilerOptions": { "module": "esnext", "moduleResolution": "bundler", "resolveJsonModule": true } }
Dead Ends
Common approaches that don't work:
-
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
Preceded by:
Frequently confused with: