Small and Fast TOML Parser
smol-toml is a JavaScript and TypeScript library designed for parsing and serializing TOML (Tom's Obvious, Minimal Language) documents. Currently at version 1.6.1, it offers a small, fast, and highly compliant implementation of the TOML specification, supporting the latest TOML 1.1.0 standard since version 1.6.0. It maintains a regular release cadence with recent updates addressing security vulnerabilities and feature enhancements. Differentiating itself from other often outdated or unmaintained TOML parsers in the JavaScript ecosystem, smol-toml is noted as the most downloaded TOML parser on npm, actively used in production systems. While generally robust, it explicitly notes some non-compliance with the `toml-test` suite for performance reasons, such as not rejecting invalid UTF-8 or certain invalid dates. It also provides options for handling integers as BigInts to prevent precision loss, a feature introduced in v1.4.0.
Common errors
-
Error: Cannot stringify value: undefined
cause Attempting to stringify a JavaScript array that contains `undefined` or `null` elements.fixFilter `undefined` or `null` values from arrays before passing the object to `smol-toml`'s `stringify` function. Example: `stringify({ my_array: [1, null, 3].filter(item => item !== null) })`. -
JavaScript number precision limits exceeded for large integer.
cause A TOML document contains an integer larger than `Number.MAX_SAFE_INTEGER` (2^53 - 1) which is parsed into a standard JavaScript `number` by default, leading to data corruption.fixWhen parsing TOML, enable BigInt support by calling `parse(tomlString, { parseBigInt: true })`. This will parse large integers as JavaScript `BigInt` types, preserving their full precision. -
Error: Cannot stringify value: [Function: myFunction]
cause An object passed to `stringify` contains a JavaScript `Function`, `Class`, or `Symbol` type, which `smol-toml` does not support for serialization.fixBefore calling `stringify`, ensure that the object only contains serializable primitive types, arrays, and plain objects. Remove or transform any `Function`, `Class`, or `Symbol` properties.
Warnings
- breaking The stringify function's output format was significantly improved in v1.5.0 to be more compact by removing unnecessary table headers and empty lines between successive table headers. This change might affect tests or tools that rely on the exact previous output structure.
- breaking smol-toml v1.6.1 addresses a security vulnerability (GHSA-v3rj-xjv7-4jmq) where specially crafted TOML documents with thousands of successive commented lines could cause an unrestricted recursion leading to a stack overflow error. All users should update immediately.
- gotcha By default, both integers and floats are parsed into standard JavaScript `number` types. This means that integers larger than 53 bits (Number.MAX_SAFE_INTEGER) will lose precision. To preserve full type information and handle large integers, you must enable the `parseBigInt` option.
- gotcha When stringifying objects to TOML, `undefined` and `null` values within arrays are explicitly rejected and will cause an error. `undefined` and `null` values as standalone object properties are ignored (do not produce a key/value pair).
- gotcha The `stringify` function explicitly rejects non-serializable JavaScript types such as functions, classes, and symbols. Attempting to stringify an object containing these types will result in an error.
Install
-
npm install smol-toml -
yarn add smol-toml -
pnpm add smol-toml
Imports
- parse
const parse = require('smol-toml').parseimport { parse } from 'smol-toml' - stringify
const stringify = require('smol-toml').stringifyimport { stringify } from 'smol-toml' - TOML (default export)
import { TOML } from 'smol-toml'import TOML from 'smol-toml'
Quickstart
import { parse, stringify } from 'smol-toml';
const tomlDoc = `
[database]
driver = "postgresql"
server.host = "127.0.0.1"
server.port = 3307
float_val = 1.0
int_val = 9223372036854775807
`;
console.log('--- Original TOML Document ---');
console.log(tomlDoc);
// Parse the TOML document
const parsedConfig = parse(tomlDoc, { parseBigInt: true }); // parseBigInt for large integers
console.log('\n--- Parsed JavaScript Object ---');
console.log(JSON.stringify(parsedConfig, null, 2));
// Stringify the JavaScript object back to TOML
const stringifiedToml = stringify(parsedConfig);
console.log('\n--- Stringified TOML Document ---');
console.log(stringifiedToml);
// Example with default export
import TOML from 'smol-toml';
const parsedWithDefault = TOML.parse(tomlDoc);
console.log('\n--- Parsed with default export ---');
console.log(JSON.stringify(parsedWithDefault.database, null, 2));