Loaders
Import JSON, JSONC, JSON5, TOML, YAML, and plain-text files directly as default exports, via an extension-keyed load hook.
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"; // stringExtensions
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: 5432import config from "./config.yaml"; // { host: "localhost", port: 5432 }
const { host, port } = config; // "localhost", 5432The 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.
Environment files
Automatic environment-file loading — the file set, precedence, variable expansion, the skip under the test environment, and how an explicit env-file flag disables auto-discovery.
Web Workers
A browser-style worker API on stock Node — the full constructor, messaging, and event surface, on every supported version.