Skip to content

useJsonImportAttribute

Enforces the use of with { type: "json" } for JSON module imports.

ECMAScript modules can import JSON modules. However, the specific import assertion with { type: "json" } is required to inform the JavaScript runtime that the imported file should be parsed as JSON. Omitting this assertion can lead to runtime errors or misinterpretation of the imported module.

This rule ensures that all imports of .json files include this assertion.

import jsonData from './data.json';
code-block.js:1:1 lint/nursery/useJsonImportAttribute  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This JSON import is missing the type: “json” import attribute.

> 1 │ import jsonData from ‘./data.json’;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

To explicitly declare the module type for JSON imports, add with { type: “json” } to this import statement.

Safe fix: Add ‘type: “json”’ import attribute.

1 │ import·jsonData·from·‘./data.json’·with·{·type:·json·};
++++++++++++++++++++++
import jsonData from './data.json' with { someOtherAttribute: "value" };
code-block.js:1:36 lint/nursery/useJsonImportAttribute  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The import attributes for this JSON module are missing type: “json”.

> 1 │ import jsonData from ‘./data.json’ with { someOtherAttribute: “value” };
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

Ensure the with clause includes type: “json” for this JSON import.

Safe fix: Add ‘type: “json”’ import attribute.

1 │ import·jsonData·from·‘./data.json’·with·{·type:·json,·someOtherAttribute:·“value”·};
++++++++++++++
import jsonData from './data.json' with { type: "json" };
import jsonData from './data.json' with { type: "json", other: "value" };
import code from './script.js'; // Not a JSON import
biome.json
{
"linter": {
"rules": {
"nursery": {
"useJsonImportAttribute": "error"
}
}
}
}