Loaders
Import JSON, JSONC, JSON5, TOML, YAML, and plain-text files as default exports, plus any file as raw text via an import attribute.
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.
Import any file as text
Add the with { type: "text" } import attribute to read a file's raw contents as a string, whatever its extension.
import readme from "./README.md" with { type: "text" }; // string
import query from "./query.sql" with { type: "text" }; // string
import raw from "./config.yaml" with { type: "text" }; // the YAML source, unparsedThe attribute wins over parsing: a .yaml or .json file read this way is the raw text, not the parsed value. A text import is a single default export, like the data loaders — there are no named exports.
Environment files
Automatic environment-file loading — the file set, mode selection from APP_ENV or a clamped NODE_ENV fallback, precedence, variable expansion, the skip under the test environment, loading explicit files with --env-file, and disabling all loading with --no-env-file.
Web Workers
A browser-style Worker global on stock Node — its constructor, messaging, transferables, and event surface.