Importing JSON

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

Edit on GitHub

./main.ts

JSON files can be imported in JS and TS modules. When doing so, you need to specify the `type: "json"` import attribute.

import file from "./version.json" with { type: "json" };
console.log(file.version);

Dynamic imports are also supported.

const module = await import("./version.json", {
  with: { type: "json" }
});

console.log(module.default.version);

./version.json

{
  "version": "1.0.0"
}

Run this example locally using the Ende CLI:

ende run https://docs.ende.sh/examples/scripts/importing_json.ts