bconfjs
A JavaScript implementation of the bconf configuration language.
Requires Node.js 22.18 or newer.
Usage
import bconf from "bconfjs";
const config = await bconf.load("./config.bconf");
API
The default export has three functions:
bconf.parse(source, opts?) parses source text into a document. It throws a LexError or
ParseError on the first syntax error. opts.filename is attached to error locations.
bconf.evaluate(document, opts?) evaluates a parsed document and returns a plain JavaScript
value. Each call starts from an empty scope.
bconf.load(specifier, opts?) reads a file through the file loader, then parses and evaluates
it.
Named exports:
BconfError, LexError, ParseError, EvaluationError: error classes. Each error has a
stable code from ErrorCode.
ErrorCode: every error code value, such as ErrorCode.EVAL_NO_FILE_LOADER.
formatDiagnostic(diagnostic, source?): renders a diagnostic with an optional source frame.
Options
evaluate and load accept these options:
env: environment values available to the env() modifier.
state: a mutable object shared with custom modifiers and directives.
filename: the path of the document being evaluated, used to resolve relative imports.
loadFile: the file loader (see below).
modifiers and directives: custom functions that extend or override the built-ins.
File loading
@import, @extends, and bconf.load read other files through a loader.
- Omit
loadFile to use the default loader for your environment. In Node.js it reads from disk,
resolving relative specifiers against the importing file's directory. In browsers it fetches
URLs, resolving against the importing file's URL.
- Pass
loadFile: null to disable file access. Any import then fails with
EVAL_NO_FILE_LOADER, while the document's own variables and values still work.
- Pass a function to use your own loader:
const config = await bconf.load("config.bconf", {
loadFile: (specifier, from) => ({ id: specifier, source: lookUp(specifier) }),
});
Internal API
bconfjs/internal exposes the lexer, parser, evaluator, AST node factories, built-in modifiers and
directives, and the default file loaders. Use it for custom tooling, such as editor integrations
that need diagnostics and error recovery:
import { parse } from "bconfjs/internal";
const { document, diagnostics } = parse(source, { recover: true });