deno.com

Importing JSON

在 Github 上编辑

JSON files can be imported in JS and TS files using the `import` keyword. This makes including static data in a library much easier.

JSON files can be imported in JS and TS modules. When doing so, you need to specify the "json" import assertion type.
./main.ts
import file from "./version.json" with { type: "json" };
console.log(file.version);
Dynamic imports are also supported.
./main.ts
const module = await import("./version.json", {
  with: { type: "json" },
});
console.log(module.default.version);
./version.json
{
  "version": "1.0.0"
}

使用 Deno CLI 在本地运行 此示例

deno run https://docs.denohub.com/examples/scripts/importing_json.ts/main

你找到需要的内容了吗?

隐私政策