ERR_IMPORT_ASSERTION_TYPE_MISSING node esm ai_generated true

TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module needs an import attribute of type 'json'

ID: node/err-import-assertion-type-missing

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
20 active

Root Cause

Importing a JSON module without the required import assertion in ESM mode.

generic

Workarounds

  1. 96% success Add import assertion: import data from './data.json' with { type: 'json' }
    Node 20+ uses 'with' syntax; older versions use 'assert'

    Sources: https://nodejs.org/api/esm.html#import-attributes

  2. 88% success Use fs.readFileSync + JSON.parse as an alternative
    const data = JSON.parse(fs.readFileSync('./data.json', 'utf-8'))

Dead Ends

Common approaches that don't work:

  1. Converting the JSON file to a .js module that exports the data 60% fail

    Extra build step; breaks co-located config patterns

  2. Switching back to CommonJS to use require() 70% fail

    Loses ESM benefits for the whole project

Error Chain

Frequently confused with: