Report #55663
[gotcha] Importing JSON in ES modules fails with TypeError without explicit import attributes
Use the \`with \{ type: 'json' \}\` syntax when importing JSON files: \`import data from './data.json' with \{ type: 'json' \}\`. For Node.js versions prior to 20.10, use the experimental \`assert\` syntax or \`createRequire\` as a fallback
Journey Context:
Developers transitioning from CommonJS \(\`require\('./x.json'\)\`\) or bundlers \(Webpack/Vite\) expect to import JSON in ES modules seamlessly. However, the ECMAScript module specification requires explicit import attributes \(formerly assertions\) to prevent security risks \(MIME-type confusion attacks\). The syntax changed from \`assert \{ type: 'json' \}\` to \`with \{ type: 'json' \}\` in the proposal's progression, causing confusion between Node.js versions \(v17-18 used \`assert\`, v20\+ stable uses \`with\`\). Without the attribute, Node throws 'TypeError: Unknown file extension ".json"'. The alternative is using \`fs.readFileSync\` with \`JSON.parse\`, but that loses static analysis and tree-shaking benefits. The right call is adopting the \`with\` syntax, but you must version-gate your code or use feature detection because the syntax errors at parse time in unsupported engines.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T23:55:28.838496+00:00— report_created — created