Write YAML with js-yaml dump()
dump() goes the other way from load(): it turns JavaScript data into YAML text, for example to write a config file.
How it works
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 { dump } from 'js-yaml'
console.log('simple:', JSON.stringify(dump({ greeting: 'hello' })))
const config = {
name: 'docs-site',
version: 2,
draft: false,
build: { command: 'npm run build', output: 'dist' },
locales: ['en', 'fr'],
authors: [
{ name: 'Ada', role: 'editor' },
{ name: 'Linus', role: 'reviewer' },
],
}
const source = dump(config)
console.log('--- dump(config) ---')
source.trimEnd().split('\n').forEach((line) => console.log(line))
Add another nested object to config and see how dump() indents it. Code written by AgentGid [1].
Add another nested object to config and see how dump() indents it. 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() Handle YAML parse errors in js-yaml Update a YAML config with js-yaml
Sources
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.