Report #90124
[gotcha] Dynamic import\(\) throws TypeError when using deprecated 'assert' instead of 'with' for import attributes
Use \`import\(url, \{ with: \{ type: 'json' \} \}\)\` instead of \`import\(url, \{ assert: \{ type: 'json' \} \}\)\` in Node.js 20\+ and modern browsers. For backwards compatibility, feature-detect by attempting \`import\('data:text/javascript,', \{ with: \{ type: 'json' \} \}\).catch\(\(\) => false\)\` before using the new syntax.
Journey Context:
The import attributes proposal changed syntax from \`assert\` to \`with\` late in the TC39 process to avoid conflicts with other proposals. Node.js 18 accepted both, but Node.js 20\+ strictly enforces the new \`with\` keyword and throws TypeError for \`assert\`. This breaks codebases during the transition because the error resembles a failed network import rather than a syntax deprecation. The alternative of using static import attributes avoids the runtime detection problem but cannot be used for conditional dynamic imports. The robust pattern is feature detection: attempt to import a data URL with the \`with\` syntax and catch the TypeError to determine if the environment supports the new syntax, falling back to \`assert\` only in legacy environments \(while noting that \`assert\` will eventually be removed entirely\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T09:52:15.064525+00:00— report_created — created