NPM Star
Collections
  1. Home
  2. Compare
  3. toml vs yaml
NPM Compare

Compare NPM packages statistics, trends, and features

CollectionsVS Code extensionChrome extensionTermsPrivacyLinkTreeIndiehackersBig Frontendqiuyumi

Unable to load comparison data. Please try again later.

TOML Parser for Node.js

CI

If you haven't heard of TOML, well you're just missing out. Go check it out now. Back? Good.

TOML Spec Support

toml-node supports TOML v1.1.0, scoring 702/708 (99.2%) on the official toml-test compliance suite:

| | Pass | Total | Rate | |---|---|---|---| | Valid tests | 218 | 218 | 100% | | Invalid tests | 484 | 490 | 98.8% | | Total | 702 | 708 | 99.2% |

The 6 remaining failures are inherent JavaScript platform limitations shared by all JS TOML parsers: they cover UTF-8 encoding validation, which Node.js handles at the engine level before the parser sees the data.

Note that integers beyond Number.MAX_SAFE_INTEGER require the bigint option to parse losslessly; without it they throw a parse error rather than silently losing precision.

Feature Support

  • Strings: basic, literal, multiline, all escape sequences (\uXXXX, \UXXXXXXXX, \xHH, \e)
  • Integers: decimal, hexadecimal (0xDEADBEEF), octal (0o755), binary (0b11010110)
  • Floats: decimal, scientific notation, inf, -inf, nan
  • Booleans: true, false
  • Dates/Times: offset date-time, local date-time, local date, local time; seconds optional
  • Arrays: mixed types allowed
  • Tables: standard, inline (with dotted/quoted keys, newlines, trailing commas), array of tables
  • Keys: bare, quoted, dotted (fruit.apple.color = "red")
  • Comments: # line comments

Installation

npm install toml

Requires Node.js 20 or later. Zero runtime dependencies.

Usage

const toml = require('toml'); const data = toml.parse(someTomlString);

toml.parse throws an exception on parse errors with line and column properties:

try { toml.parse(someBadToml); } catch (e) { console.error(`Parsing error on line ${e.line}, column ${e.column}: ${e.message}`); }

Nesting Depth Limit

To guard against stack overflow on maliciously deep input, arrays and inline tables may nest at most 500 levels deep by default; input past the limit throws a normal parse error. Adjust the limit with the maxDepth option:

toml.parse(someTomlString, { maxDepth: 100 });

Integer Range and BigInt

TOML requires parsers to handle the full range of 64-bit signed integers, but JavaScript's number type can only represent integers up to Number.MAX_SAFE_INTEGER (2⁵³ − 1) losslessly. By default, toml.parse returns integers as number and throws a parse error when a value falls outside the safe range, rather than silently returning a rounded value:

toml.parse('id = 771752188537605140'); // Error: Integer 771752188537605140 cannot be represented losslessly // as a JavaScript number. Use the `bigint` option to parse integers // as BigInt values.

Pass bigint: true to instead parse all integers as BigInt, preserving the full 64-bit range:

const data = toml.parse('id = 771752188537605140\ncount = 3', { bigint: true }); data.id // 771752188537605140n data.count // 3n

Integers outside the 64-bit signed range always throw, in either mode, as required by the spec. Floats are unaffected by all of this: TOML floats are IEEE 754 binary64 values, which is exactly what a JavaScript number is, so every TOML float is represented as faithfully as the spec intends.

Date/Time Values

Offset date-times are returned as JavaScript Date objects. Local date-times, local dates, and local times are returned as strings since they have no timezone information and can't be losslessly represented as Date:

const data = toml.parse(` odt = 1979-05-27T07:32:00Z # Date object ldt = 1979-05-27T07:32:00 # string: "1979-05-27T07:32:00" ld = 1979-05-27 # string: "1979-05-27" lt = 07:32:00 # string: "07:32:00" `); data.odt instanceof Date // true typeof data.ldt // "string" typeof data.ld // "string" typeof data.lt // "string"

Temporal Support

Pass useTemporal: true to have date/time values returned as Temporal objects instead:

| TOML type | Returned as | | ---------------- | ------------------------- | | Offset date-time | Temporal.ZonedDateTime | | Local date-time | Temporal.PlainDateTime | | Local date | Temporal.PlainDate | | Local time | Temporal.PlainTime |

const data = toml.parse(` odt = 1979-05-27T00:32:00-07:00 ldt = 1979-05-27T07:32:00 ld = 1979-05-27 lt = 07:32:00 `, { useTemporal: true }); data.odt.toString() // "1979-05-27T00:32:00-07:00[-07:00]" data.ldt.toString() // "1979-05-27T07:32:00" data.ld.toString() // "1979-05-27" data.lt.toString() // "07:32:00"

Offset date-times become Temporal.ZonedDateTime values whose time zone is the original UTC offset (Z maps to the UTC time zone), so the offset written in the TOML document is preserved — unlike the default Date representation, which loses it. Fractional seconds beyond nanosecond precision are truncated, as permitted by the TOML spec.

useTemporal requires a runtime with the Temporal global. On runtimes that don't provide it yet, pass an implementation such as @js-temporal/polyfill via the temporal option:

const { Temporal } = require('@js-temporal/polyfill'); const data = toml.parse(someTomlString, { useTemporal: true, temporal: Temporal });

Once Temporal is broadly available, Temporal output is expected to become the default behavior in a future major version.

Special Float Values

inf and nan are returned as JavaScript Infinity and NaN:

const data = toml.parse(` pos_inf = inf neg_inf = -inf not_a_number = nan `); data.pos_inf === Infinity // true data.neg_inf === -Infinity // true Number.isNaN(data.not_a_number) // true

Requiring .toml Files

You can use the toml-require package to require() your .toml files with Node.js.

Building & Testing

toml-node uses the Peggy parser generator (successor to PEG.js).

npm install
npm run build
npm test
npm run test:spec           # run toml-test compliance suite
npm run test:spec:failures  # show failure details

Changes to src/toml.pegjs require a rebuild with npm run build.

License

toml-node is licensed under the MIT license agreement. See the LICENSE file for more information.

YAML <a href="https://www.npmjs.com/package/yaml"><img align="right" src="https://badge.fury.io/js/yaml.svg" title="npm package" /></a>

yaml is a definitive library for YAML, the human friendly data serialization standard. This library:

  • Supports both YAML 1.1 and YAML 1.2 and all common data schemas,
  • Passes all of the yaml-test-suite tests,
  • Can accept any string as input without throwing, parsing as much YAML out of it as it can, and
  • Supports parsing, modifying, and writing YAML comments and blank lines.

The library is released under the ISC open source license, and the code is available on GitHub. It has no external dependencies and runs on Node.js as well as modern browsers.

For the purposes of versioning, any changes that break any of the documented endpoints or APIs will be considered semver-major breaking changes. Undocumented library internals may change between minor versions, and previous APIs may be deprecated (but not removed).

The minimum supported TypeScript version of the included typings is 3.9; for use in earlier versions you may need to set skipLibCheck: true in your config. This requirement may be updated between minor versions of the library.

For more information, see the project's documentation site: eemeli.org/yaml

For build instructions and contribution guidelines, see docs/CONTRIBUTING.md.

To install:

npm install yaml # or deno add jsr:@eemeli/yaml

Note: These docs are for yaml@2. For v1, see the v1.10.0 tag for the source and eemeli.org/yaml/v1 for the documentation.

API Overview

The API provided by yaml has three layers, depending on how deep you need to go: Parse & Stringify, Documents, and the underlying Lexer/Parser/Composer. The first has the simplest API and "just works", the second gets you all the bells and whistles supported by the library along with a decent AST, and the third lets you get progressively closer to YAML source, if that's your thing.

A command-line tool is also included.

Parse & Stringify

import { parse, stringify } from 'yaml'
  • parse(str, reviver?, options?): value
  • stringify(value, replacer?, options?): string

Documents

<!-- prettier-ignore -->
import { Document, isDocument, parseAllDocuments, parseDocument } from 'yaml'
  • Document
    • constructor(value, replacer?, options?)
    • #contents
    • #directives
    • #errors
    • #warnings
  • isDocument(foo): boolean
  • parseAllDocuments(str, options?): Document[]
  • parseDocument(str, options?): Document

Content Nodes

<!-- prettier-ignore -->
import { isAlias, isCollection, isMap, isNode, isPair, isScalar, isSeq, Scalar, visit, visitAsync, YAMLMap, YAMLSeq } from 'yaml'
  • isAlias(foo): boolean
  • isCollection(foo): boolean
  • isMap(foo): boolean
  • isNode(foo): boolean
  • isPair(foo): boolean
  • isScalar(foo): boolean
  • isSeq(foo): boolean
  • new Scalar(value)
  • new YAMLMap()
  • new YAMLSeq()
  • doc.createAlias(node, name?): Alias
  • doc.createNode(value, options?): Node
  • doc.createPair(key, value): Pair
  • visit(node, visitor)
  • visitAsync(node, visitor)

Parsing YAML

import { Composer, Lexer, Parser } from 'yaml'
  • new Lexer().lex(src)
  • new Parser(onNewLine?).parse(src)
  • new Composer(options?).compose(tokens)

YAML.parse

# file.yml YAML: - A human-readable data serialization language - https://en.wikipedia.org/wiki/YAML yaml: - A complete JavaScript implementation - https://www.npmjs.com/package/yaml
import fs from 'fs' import YAML from 'yaml' YAML.parse('3.14159') // 3.14159 YAML.parse('[ true, false, maybe, null ]\n') // [ true, false, 'maybe', null ] const file = fs.readFileSync('./file.yml', 'utf8') YAML.parse(file) // { YAML: // [ 'A human-readable data serialization language', // 'https://en.wikipedia.org/wiki/YAML' ], // yaml: // [ 'A complete JavaScript implementation', // 'https://www.npmjs.com/package/yaml' ] }

YAML.stringify

import YAML from 'yaml' YAML.stringify(3.14159) // '3.14159\n' YAML.stringify([true, false, 'maybe', null]) // `- true // - false // - maybe // - null // ` YAML.stringify({ number: 3, plain: 'string', block: 'two\nlines\n' }) // `number: 3 // plain: string // block: | // two // lines // `

Browser testing provided by:

<a href="https://www.browserstack.com/open-source"> <img width=200 src="https://eemeli.org/yaml/images/browserstack.svg" alt="BrowserStack" /> </a>

Dependencies Comparison

toml

Dependencies

Dev Dependencies

@js-temporal/polyfill^0.5.1
peggy^5.1.0

Peer Dependencies

yaml

Dependencies

Dev Dependencies

@babel/core^7.12.10
@babel/plugin-transform-typescript^7.12.17
@babel/preset-env^7.12.11
@eslint/js^9.9.1
@rollup/plugin-babel^6.0.3
@rollup/plugin-replace^6.0.3
@rollup/plugin-typescript^12.1.1
@types/jest^29.2.4
@types/node^20.11.20
babel-jest^29.0.1
eslint^9.9.1
eslint-config-prettier^10.1.8
fast-check^2.12.0
jest^29.0.1
jest-resolve^29.7.0
jest-ts-webcompat-resolver^1.0.0
prettier^3.0.2
rollup^4.12.0
tslib^2.8.1
typescript^5.7.2
typescript-eslint^8.4.0

Peer Dependencies

StarsIssuesVersionUpdatedⓘLast publish dateCreatedⓘPackage creation dateSizeⓘMinified + Gzipped size
T
toml
32505.0.0a month ago13 years agoinstall size 8.2 KB
Y
yaml
1,682392.9.02 months ago15 years agoinstall size 30.5 KB

Who's Using These Packages

toml

react-icons
react-icons

svg react icons of popular icon packs

livestore
livestore

LiveStore is a next-generation state management framework based on reactive SQLite and built-in sync engine.

ogs
ogs

DO NOT USE THIS REPO! Migrated to https://gitlab.opengeosys.org/ogs/ogs!

Dependency
Dependency
eslint-online-playground
eslint-online-playground

ESLint Online Playground

yaml

magento2
magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.

livestore
livestore

LiveStore is a next-generation state management framework based on reactive SQLite and built-in sync engine.

api-guidelines
api-guidelines

adidas group API design guidelines

fabrica-dev-kit
fabrica-dev-kit

A toolkit for faster, smoother WordPress 5 development

tryhackme-ctf
tryhackme-ctf

TryHackMe CTFs writeups, notes, drafts, scrabbles, files and solutions.