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

ERR_IMPORT_ASSERTION_TYPE_MISSING

Nei moduli ESM l'importazione di un file JSON richiede obbligatoriamente l'attributo di tipo di sicurezza: 'import data from "./data.json" with { type: "json" };' (o la sintassi precedente 'assert { type: "json" }').

Cosa significa questo errore?

Nei moduli ESM l'importazione di un file JSON richiede obbligatoriamente l'attributo di tipo di sicurezza: 'import data from "./data.json" with { type: "json" };' (o la sintassi precedente 'assert { type: "json" }').

Messaggio originale

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

Contesto e parole chiave

Alias

  • import assertion type json missing Node.js
  • import attributes with type json

Messaggi alternativi

  • attributo di importazione type json mancante per file JSON in ESM

Prodotti

  • Node.js Runtime (v18 LTS, v20 LTS, v22 LTS, v24)

Piattaforme

  • Linux
  • macOS
  • Windows

Parole chiave

  • errore-nodejs-err-import-assertion-type-missing-json
  • javascript-backend
  • Moduli e CommonJS ed ESM
  • node
  • nodejs
  • nodejs.moduli.err-import-assertion-type-missing-json

Cause possibili

Causa tecnica per Errore Node.js: Module needs an import attribute of type "json" (Importazione File JSON in ESM)

Nei moduli ESM l'importazione di un file JSON richiede obbligatoriamente l'attributo di tipo di sicurezza: 'import data from "./data.json" with { type: "json" };' (o la sintassi precedente 'assert { type: "json" }').

molto comune

Diagnosi

Diagnostica di runtime o stack trace per Errore Node.js: Module needs an import attribute of type "json" (Importazione File JSON in ESM)

Eseguire Node.js con i flag diagnostici (--trace-warnings --trace-uncaught) per visualizzare l'origine dettagliata dell'eccezione.

Come leggere il risultato. Evidenzia il modulo esatto, la riga e lo stack trace della chiamata asincrona in errore.

terminal
node --trace-warnings --trace-uncaught app.js

Risoluzione

Risoluzione standard per Errore Node.js: Module needs an import attribute of type "json" (Importazione File JSON in ESM)

Difficoltà: mediaRischio: basso

Applicare la correzione al codice sorgente Node.js o alla configurazione del package.json.

  1. Ispezionare la riga evidenziata dal codice di errore o stack trace
  2. Aggiornare la sintassi di import/export, il controllo dei socket o la gestione della memoria
  3. Riavviare il processo Node.js o il demone di produzione (PM2/systemd) per verificare la stabilità
terminal
node app.js

Domande frequenti

Come risolvere l'errore Errore Node.js: Module needs an import attribute of type "json" (Importazione File JSON in ESM) in Node.js?

Nei moduli ESM l'importazione di un file JSON richiede obbligatoriamente l'attributo di tipo di sicurezza: 'import data from "./data.json" with { type: "json" };' (o la sintassi precedente 'assert { type: "json" }').

Approfondimenti

Schede correlate