JSON 模块


【JSON modules】

JSON 文件可以通过 import 引用:

【JSON files can be referenced by import:】

import packageConfig from './package.json' with { type: 'json' }; 

必须使用 with { type: 'json' } 语法;详见 导入属性

【The with { type: 'json' } syntax is mandatory; see Import Attributes.】

导入的 JSON 只暴露了 default 导出。不支持命名导出。为了避免重复,会在 CommonJS 缓存中创建一个缓存项。如果 JSON 模块已从相同路径导入,CommonJS 会返回同一个对象。

【The imported JSON only exposes a default export. There is no support for named exports. A cache entry is created in the CommonJS cache to avoid duplication. The same object is returned in CommonJS if the JSON module has already been imported from the same path.】