This recipe converts import assertions (assert syntax) to the standardized import attributes (with syntax) for importing JSON and other module types.
Background
Node.js dropped support for import assertions in favor of import attributes in version 22.0.0. The new syntax uses with instead of assert to align with the ECMAScript standard.
Import attributes were added in Node.js version 18.20.0.
Usage
Run this codemod with:
Before/After
What It Does
- Replaces
assert { type: 'json' } with with { type: 'json' }
- Updates both static and dynamic imports
- Preserves import path and other syntax
When to Use This
Use this codemod when:
- Upgrading to Node.js 22 or later
- Your codebase uses import assertions for JSON modules
- You want to adopt the standardized ECMAScript syntax
Example Scenarios
JSON Imports
Dynamic Imports
Import assertions are completely removed in Node.js 22. If you’re running Node.js 22+, your code will fail without this migration.
The with keyword is the standard syntax across all JavaScript runtimes. Migrating now ensures better cross-runtime compatibility.