// export.js exportconst name = 'nodejs'; // main.js let name = 'esm import' import {name as exportName} from'./export.js' console.log(exportName) // nodejs
ESM无法导入json文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import data from'./data.json' console.log(data);
TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module"file:///Users/xiuji/Desktop/workplace/own/Node/data.json" needs an import assertion of type "json" at newNodeError (node:internal/errors:372:5) at validateAssertions (node:internal/modules/esm/assert:82:15) at defaultLoad (node:internal/modules/esm/load:24:3) at ESMLoader.load (node:internal/modules/esm/loader:407:26) at ESMLoader.moduleProvider (node:internal/modules/esm/loader:326:22) at newModuleJob (node:internal/modules/esm/module_job:66:26) at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:345:17) at ESMLoader.getModuleJob (node:internal/modules/esm/loader:304:34) at asyncModuleWrap.<anonymous> (node:internal/modules/esm/module_job:82:21) at asyncPromise.all (index 0) { code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING' } ELIFECYCLECommand failed with exit code 1.