Registry / serialization / yaml.macro

yaml.macro

JSON →
library0.2.0jsnpmunverified

A Babel macro for loading YAML files at build time, transforming YAML into JavaScript objects via babel-plugin-macros. Current stable version is 0.2.0, released infrequently. It uses the `yaml` package under the hood and supports all its parser options. Key differentiators: integrates seamlessly with Babel macros, evaluated at build time (not runtime), supports any YAML 1.2 types. Unlike runtime YAML loaders, this macro inlines data at compile time, reducing bundle size and runtime overhead. Requires Node.js >=10 and babel-plugin-macros.

npm install yaml.macro
INSTALL
IMPORT
SIG · YAML.MACRO
Y
yaml.macro
serializationjavascriptv0.2.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–223 runs
build_error
glibc
node 18–223 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
✓ import yaml from 'yaml.macro'
✗ const yaml = require('yaml.macro')
The macro is a default export and must be imported as ESM. CommonJS require will not work because Babel macros are only processed by Babel when using import syntax.
yaml
✓ import yaml from 'yaml.macro'; yaml('./file.yaml')
✗ import { yaml } from 'yaml.macro'
There is no named export; the function is the default export. Using named import will result in undefined.
yaml with options
✓ import yaml from 'yaml.macro'; yaml('./file.yaml', { schema: 'core' })
✗ import yaml from 'yaml.macro'; yaml('./file.yaml', { schema: 'core' }, extraArg)
The macro only accepts two arguments: path and optional options object. Passing extra arguments or dynamic values may cause evaluation errors.

Shows setup with babel-plugin-macros, a YAML file, and importing/using yaml.macro to inline YAML content at build time.

// Install: npm install --save-dev yaml.macro babel-plugin-macros // .babelrc or babel.config.js: { "plugins": ["macros"] } // myData.yaml: // - item1 // - item2: value // myFile.js: import yaml from 'yaml.macro'; const myData = yaml('./myData.yaml'); console.log(myData); // ['item1', { item2: 'value' }] export default myData;
Debug
Known issues
gotchaThe path argument must be a string literal starting with '.' for relative paths or an absolute path. Dynamic expressions or variables are not evaluated at build time and will cause errors.
fix
Use a constant string: yaml('./myfile.yaml') not yaml(filePathVariable)
affects: >=0.1.0
gotchaMultiple calls to the same YAML file are not cached. Each call will re-parse the file, increasing build time.
fix
Assign the result to a variable and reuse it: const data = yaml('./file.yaml'); then use data multiple times.
affects: >=0.1.0
breakingVersion 0.2.0 requires Node.js >=10. Older versions (0.1.x) may work on Node 8 but are unsupported.
fix
Upgrade Node.js to version 10 or higher.
affects: >=0.2.0
deprecatedThe macro uses the 'yaml' package version 2 internally. If you rely on specific parsing behavior from older versions, check compatibility.
fix
Refer to yaml package changelog for breaking changes between versions.
affects: >=0.2.0
Errors
Common errors & fixes
yaml.macro is not a function
Attempting to use require() or named import instead of default import.
fix
Use import yaml from 'yaml.macro'
Cannot find module 'babel-plugin-macros'
Missing babel-plugin-macros package or not installed as dev dependency.
fix
npm install --save-dev babel-plugin-macros
The macro threw an error: 'path' is not defined
Passing a variable or expression as the path argument instead of a string literal.
fix
yaml('./your-file.yaml') — path must be a string literal
Upgrade
Version history
0.2.0latest on npm
Audit
Dependencies
babel-plugin-macrosrequiredrequired peer dependency for Babel macro functionality
yamlrequiredYAML parser used internally
Agent activity
31 hits · last 30 days
node
26
OpenAI (training)
1
Resources
yaml.macro — npm install yaml.macro · libregistry