JSON import with assert stops working after upgrading to Node.js 22
SyntaxError: Unexpected identifier 'assert'Short answer
Node.js 22 dropped import assertions. The keyword is now with, so import data from './data.json' assert { type: 'json' } becomes import data from './data.json' with { type: 'json' }. The type attribute is mandatory for JSON, and a JSON module has only a default export, so named imports fail too.
Fix it fast
1Find out which cause you have
Confirm which Node.js runs the file, then search the project for the old keyword.
node --version grep -rn "assert { type" --include=*.js --include=*.mjs --include=*.ts src/2Match what you see, then jump to its fix
- 1
SyntaxError: Unexpected identifier 'assert' on a line that imports a .json file, only on Node.js 22 or newerThe code still uses the old assert keywordFix - 2
An import of a .json file fails even though the syntax is valid ESM, and the error code is ERR_IMPORT_ATTRIBUTE_MISSINGThe type attribute is missing from a JSON importFix - 3
The import line has braces, such as import { name } from './package.json' with { type: 'json' }A named import is used on a JSON moduleFix - 4
A tool in the chain still cannot parse withYou would rather not use import attributes at allFix
- 1
What does the error look like?
SyntaxError: Unexpected identifier 'assert'import config from './config.json' assert { type: 'json' };
Cause 1 of 4
The code still uses the old assert keyword
1Check
The Node.js ESM docs list the change in their history table: Node.js 21.0.0, 20.10.0 and 18.20.0 switched from import assertions to import attributes, and 22.0.0 dropped support for assertions.
node --version2Fix
Replace assert with with. Do the same for dynamic imports, where the option is called with.
// static import
import packageConfig from './package.json' with { type: 'json' };
// dynamic import
const { default: data } = await import('./data.json', { with: { type: 'json' } });3Verify
Run the entry file again. The SyntaxError is gone.
node src/index.mjsConfirmed by nodejs.org
Cause 2 of 4
The type attribute is missing from a JSON import
1Check
The docs say the type: 'json' attribute is mandatory when importing JSON modules. The errors page defines ERR_IMPORT_ATTRIBUTE_MISSING as an import attribute that is missing, which prevents the module from being imported.
grep -rn "from '.*\.json'" src/2Fix
Add with { type: 'json' } to every JSON import.
import settings from './settings.json' with { type: 'json' };3Verify
Print one value from the imported object.
node --input-type=module -e "import s from './settings.json' with { type: 'json' }; console.log(s)"Confirmed by nodejs.org
Cause 3 of 4
A named import is used on a JSON module
1Check
The docs state that a JSON module exposes only a default export, with no named exports.
grep -rn "import {.*} from '.*\.json'" src/2Fix
Import the default value and read properties from it.
import pkg from './package.json' with { type: 'json' };
console.log(pkg.name);3Verify
Run the file and read the property.
node src/index.mjsConfirmed by nodejs.org
Cause 4 of 4
You would rather not use import attributes at all
1Check
The docs describe module.createRequire() as a way to get a require function inside an ES module.
node --version2Fix
Build a require function and load the JSON through it.
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const config = require('./config.json');3Verify
Run the file and print config.
node src/index.mjsConfirmed by nodejs.org
How do I stop it coming back?
- Pin the Node.js version in .nvmrc or package.json engines so local, CI and production run the same one.
- Search for assert { type before a Node.js upgrade, not after it.
- Prefer with in all new code, since Node.js 18.20 and later already accept it.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix