|
| 1 | +# YAML |
| 2 | + |
| 3 | +<!--introduced_in=REPLACEME--> |
| 4 | + |
| 5 | +<!-- YAML |
| 6 | +added: REPLACEME |
| 7 | +--> |
| 8 | + |
| 9 | +> Stability: 1.0 - Early development |
| 10 | +
|
| 11 | +<!-- source_link=lib/yaml.js --> |
| 12 | + |
| 13 | +The `node:yaml` module parses [YAML 1.2][] into JavaScript values. Enable it |
| 14 | +with [`--experimental-yaml`][] and import it with `node:`: |
| 15 | + |
| 16 | +```mjs |
| 17 | +import yaml from 'node:yaml'; |
| 18 | +``` |
| 19 | + |
| 20 | +```cjs |
| 21 | +const yaml = require('node:yaml'); |
| 22 | +``` |
| 23 | + |
| 24 | +Loading the module emits an `ExperimentalWarning`. |
| 25 | + |
| 26 | +## `yaml.parse(input)` |
| 27 | + |
| 28 | +<!-- YAML |
| 29 | +added: REPLACEME |
| 30 | +--> |
| 31 | + |
| 32 | +* `input` {string|ArrayBuffer|SharedArrayBuffer|TypedArray|DataView} The YAML |
| 33 | + text. Binary input is decoded as UTF-8. |
| 34 | +* Returns: {any} The value of the document, or an array with the value of each |
| 35 | + document when the stream holds more than one. An empty stream yields `null`. |
| 36 | + |
| 37 | +`parse()` uses the YAML 1.2 [core schema][]. Plain scalars resolve to null, |
| 38 | +booleans, numbers or strings. For example, `yes` and `no` are strings. Quoted |
| 39 | +and block scalars are strings unless a tag changes their type, as in |
| 40 | +`!!int "42"`. Mappings become plain objects and sequences become arrays, as |
| 41 | +with [`JSON.parse()`][]. |
| 42 | + |
| 43 | +```mjs |
| 44 | +import yaml from 'node:yaml'; |
| 45 | + |
| 46 | +const config = yaml.parse(` |
| 47 | +name: app |
| 48 | +ports: |
| 49 | + - 8080 |
| 50 | + - 8443 |
| 51 | +tls: true |
| 52 | +`); |
| 53 | +console.log(config); |
| 54 | +// { name: 'app', ports: [ 8080, 8443 ], tls: true } |
| 55 | + |
| 56 | +console.log(yaml.parse('--- 1\n--- 2\n')); |
| 57 | +// [ 1, 2 ] |
| 58 | +``` |
| 59 | + |
| 60 | +Aliases to a collection refer to the same object. A collection may contain an |
| 61 | +alias to itself, so the result can contain cycles. |
| 62 | + |
| 63 | +The parser applies the core tags `!!str`, `!!int`, `!!float`, `!!bool`, |
| 64 | +`!!null`, `!!map` and `!!seq`. A tag must match its value: `!!int abc` throws |
| 65 | +an error. Other tags leave collections unchanged and make scalars strings. |
| 66 | +For example, `!custom 123` becomes `'123'`. |
| 67 | + |
| 68 | +Mapping keys become property names. Scalar keys use their string form, so |
| 69 | +`null`, `true` and `1` become `'null'`, `'true'` and `'1'`. Collection keys use |
| 70 | +their JSON text and are limited to 1024 characters. Duplicate keys throw. |
| 71 | + |
| 72 | +Invalid YAML throws an [`ERR_INVALID_YAML`][] `SyntaxError` with the line and |
| 73 | +column. The parser rejects disallowed control characters and nesting deeper |
| 74 | +than 1000 levels. |
| 75 | + |
| 76 | +[YAML 1.2]: https://yaml.org/spec/1.2.2/ |
| 77 | +[`--experimental-yaml`]: cli.md#--experimental-yaml |
| 78 | +[`ERR_INVALID_YAML`]: errors.md#err_invalid_yaml |
| 79 | +[`JSON.parse()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse |
| 80 | +[core schema]: https://yaml.org/spec/1.2.2/#103-core-schema |
0 commit comments