Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

JSON import with assert stops working after upgrading to Node.js 22

ErrorSyntaxError: 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.

  • Applies to Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/
  2. 2Match what you see, then jump to its fix

    1. 1SyntaxError: 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. 2An 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. 3The import line has braces, such as import { name } from './package.json' with { type: 'json' }A named import is used on a JSON moduleFix
    4. 4A tool in the chain still cannot parse withYou would rather not use import attributes at allFix

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 --version

2Fix

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.mjs

Confirmed by nodejs.org

Did this fix it?

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

Did this fix it?

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.mjs

Confirmed by nodejs.org

Did this fix it?

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 --version

2Fix

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.mjs

Confirmed by nodejs.org

Did this fix it?

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.