Moduli e CommonJS ed ESM
TypeError [ERR_IMPORT_ATTRIBUTE_MISSING]: Module "file.json" needs an import attribute of type "json" – Node.js 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 comuneDiagnosi
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.
node --trace-warnings --trace-uncaught app.jsRisoluzione
Risoluzione standard per Errore Node.js: Module needs an import attribute of type "json" (Importazione File JSON in ESM)
Applicare la correzione al codice sorgente Node.js o alla configurazione del package.json.
- Ispezionare la riga evidenziata dal codice di errore o stack trace
- Aggiornare la sintassi di import/export, il controllo dei socket o la gestione della memoria
- Riavviare il processo Node.js o il demone di produzione (PM2/systemd) per verificare la stabilità
node app.jsDomande 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
- Node.js Official Documentation / Node.js Errors Reference documentazione_ufficiale