Skip to main content
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:
npx codemod nodejs/import-assertions-to-attributes

Before/After

- import data from './data.json' assert { type: 'json' };
+ import data from './data.json' with { type: 'json' };

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

- import config from './config.json' assert { type: 'json' };
+ import config from './config.json' with { type: 'json' };

Dynamic Imports

- const data = await import('./data.json', { assert: { type: 'json' } });
+ const data = await import('./data.json', { with: { type: 'json' } });
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.

Build docs developers (and LLMs) love