Skip to content
Agents tracked: 440 Downloads (7d): 202M down 19.5% GitHub stars: 10.4M Releases (7d): 607 Agent status: 3 with issuesSince last limit reset: Claude 28d · Codex 0d Updated Oct 21, 2026

Handle YAML parse errors in js-yaml

YAML written by people often contains mistakes, so code that loads it should catch errors and report them clearly.

All js-yaml examples js-yaml: usage and stats

Handle YAML parse errors in js-yaml: screenshot of the example below running
The code below, running in Chromium on our server (Oct 12, 2026). Press Run to see it live and change it.

How it works

  1. Wrap load in try/catch. The README calls load inside a try block with a catch for errors. The example puts that pattern in a small safeLoad helper. [1]
  2. Compare inputs. The helper runs on several snippets: valid ones print their data, broken ones print the first line of the error message instead of stopping the script. [1]

Run and edit it

It runs in your browser, in a sandboxed frame that cannot see this site; the packages load from esm.sh only after you press Run.

import { load } from 'js-yaml'

function safeLoad(label, text) {
  try {
    const document = load(text)
    console.log('OK   ' + label + ' -> ' + JSON.stringify(document))
  } catch (e) {
    console.log('FAIL ' + label + ' -> ' + e.message.split('\n')[0])
  }
}

safeLoad('simple map', 'greeting: hello')
safeLoad('list', '- a\n- b\n- c')
safeLoad('unclosed list', 'items: [1, 2, 3')
safeLoad('bad indent', 'server:\n  host: a\n port: 80')
safeLoad('duplicate key', 'name: a\nname: b')
safeLoad('unclosed quote', 'title: "hello')
safeLoad('nested', 'user:\n  name: Ada\n  tags: [x, y]')
Ctrl/⌘ + Enter runs

Free for signed-in members, with a daily limit. Describe the change in plain words; the new code runs only in your browser.

Fix one of the broken snippets and watch it move from the error list to the OK list. Code written by AgentGid [1].

Checked Oct 12, 2026: it starts without errors in Chromium with js-yaml 5.4.3. Needs WebGL.

More js-yaml examples

Parse YAML with js-yaml load() Write YAML with js-yaml dump() Update a YAML config with js-yaml

Sources

  1. js-yaml README (GitHub)

Explanations written by AgentGid from the official docs; the numbers in brackets point here. The code marked "written by AgentGid" is ours and free to copy.

Building something with js-yaml? Ask AgentGid →