Sub topic 10
From assert to with: Migrating Legacy Import Assertions
This chapter assumes you know how the with { type: "json" } clause works on static and dynamic imports. You will learn to recognize the older assert syntax, understand why it was replaced, and convert a codebase to the current form.
What the legacy syntax looked like
Before import attributes were standardized, the proposal was called import assertions, and it used the assert keyword. Chrome 91 and Node.js 17 shipped it, so a lot of code and many tutorials still contain it:
// Legacy import assertions (rejected by current runtimes)
// import config from "./config.json" assert { type: "json" };
// Current import attributes
import config from "./config.json" with { type: "json" };The attribute object itself is unchanged. Only the keyword is different.
Why the keyword changed
An assertion could only check a module after it loaded. It was not allowed to change how the host fetched or interpreted the module. That model did not fit the web. Browsers need the type to decide how to request the resource, for example which Accept header and request destination to use. They also need it as part of the module cache key. The semantics therefore changed from "check this" to "load it this way." In 2023 TC39 renamed the feature to import attributes and replaced assert with with to match. The proposal reached Stage 4 in 2024 and is part of ES2025.
Where assert appears
Each import form had its own assert variant. Every one of them needs converting:
| Form | Legacy | Current |
|---|---|---|
| Static import | `import x from "./a.json" assert { type: "json" }` | `import x from "./a.json" with { type: "json" }` |
| Dynamic import | `import("./a.json", { assert: { type: "json" } })` | `import("./a.json", { with: { type: "json" } })` |
| Re-export | `export { default } from "./a.json" assert { type: "json" }` | `export { default } from "./a.json" with { type: "json" }` |
Dynamic imports are the easiest to miss. The key inside the options object changes, and a text search for assert { will not find it.
async function loadLocale(lang) {
const module = await import(`./locales/${lang}.json`, {
with: { type: "json" } // was: assert: { type: "json" }
});
return module.default;
}Finding and fixing occurrences
Search for both patterns, then rewrite them. This example uses GNU sed. On macOS, use sed -i ''.
grep -rnE 'assert[[:space:]]*\{|assert:[[:space:]]*\{' src/
grep -rlE 'assert[[:space:]]*\{|assert:[[:space:]]*\{' src/ \
| xargs sed -i -E \
-e 's/\)?[[:space:]]assert[[:space:]]*\{[[:space:]]*type:/ with { type:/g' \
-e 's/assert:[[:space:]]*\{[[:space:]]*type:/with: { type:/g'Review the diff before you commit. A blind replacement can also hit unrelated code, such as test helpers named assert.
Updating tooling
Tools that parse your code must understand with too:
- TypeScript: version 5.3 and later parses
with. Upgrade before you migrate. - Babel: replace
@babel/plugin-syntax-import-assertionswith@babel/plugin-syntax-import-attributes. ItsdeprecatedAssertSyntaxoption temporarily accepts both forms during a gradual migration. - ESLint: the default parser handles
withwhenecmaVersionis2025or"latest". - Bundlers: current versions of esbuild, Rollup, Vite, and webpack accept
with. Older versions may only acceptassert.
Exercise
A project has const data = await import("./data.json", { assert: { type: "json" } }); and it throws on Node.js 22. Rewrite it, and explain why a search for "assert {" missed it.
Show answer
Write const data = await import("./data.json", { with: { type: "json" } }); and read the parsed value from data.default. In the dynamic form, assert is an object key followed by a colon (assert: {), so a search for "assert {" does not match it.
Exercise
Why did TC39 rename assertions to attributes instead of keeping the assert keyword?
Show answer
Assertions could only check a module after it loaded and could not change how it was fetched. Hosts such as browsers need the type to decide how to request the module and how to key it in the module cache. The semantics became "load it this way," and the keyword changed to with to reflect that.