TypeScript setup

The features on this page (the .toml / .yaml loaders) require nub's global type-declaration package to avoid TypeScript compiler errors.

# install the package
npm i -D @nubjs/types @types/node@25

# tsconfig.json
{ "compilerOptions": { "types": ["node", "@nubjs/types"] } }

Import a config or data file directly.

import config from "./config.yaml";  // parsed object
import flags from "./feature.jsonc"; // parsed object (comments stripped)
import pkg from "./Cargo.toml";      // parsed object
import schema from "./schema.json5"; // parsed object (JSON5 superset)
import prompt from "./prompt.txt";   // string

Extensions

Loaders are keyed on file extension:

  • .jsonc — JSON with comments
  • .json5 — the JSON5 superset
  • .toml
  • .yaml / .yml
  • .txt — loaded as a string, no parsing

The .json extension is intentionally not in this set — it is Node-native (resolveJsonModule), so Nub leaves it to Node.

Default export

A data module exposes a single default export — the parsed value, exactly like Node's own JSON modules. There are no named exports; destructure the default to pull out top-level keys.

# config.yaml
host: localhost
port: 5432
import config from "./config.yaml"; // { host: "localhost", port: 5432 }
const { host, port } = config;      // "localhost", 5432

The object formats are typed Record<string, unknown>, so destructured keys are unknown — narrow or cast them at the use site. A data file whose top-level value is an array or scalar imports as that value via the default; cast it, since the wildcard type assumes an object.