{"id":20879,"library":"yaml.macro","title":"yaml.macro","description":"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.","status":"active","version":"0.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/eemeli/yaml.macro","tags":["javascript","YAML","babel-plugin-macros"],"install":[{"cmd":"npm install yaml.macro","lang":"bash","label":"npm"},{"cmd":"yarn add yaml.macro","lang":"bash","label":"yarn"},{"cmd":"pnpm add yaml.macro","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"required peer dependency for Babel macro functionality","package":"babel-plugin-macros","optional":false},{"reason":"YAML parser used internally","package":"yaml","optional":false}],"imports":[{"note":"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.","wrong":"const yaml = require('yaml.macro')","symbol":"default","correct":"import yaml from 'yaml.macro'"},{"note":"There is no named export; the function is the default export. Using named import will result in undefined.","wrong":"import { yaml } from 'yaml.macro'","symbol":"yaml","correct":"import yaml from 'yaml.macro'; yaml('./file.yaml')"},{"note":"The macro only accepts two arguments: path and optional options object. Passing extra arguments or dynamic values may cause evaluation errors.","wrong":"import yaml from 'yaml.macro'; yaml('./file.yaml', { schema: 'core' }, extraArg)","symbol":"yaml with options","correct":"import yaml from 'yaml.macro'; yaml('./file.yaml', { schema: 'core' })"}],"quickstart":{"code":"// Install: npm install --save-dev yaml.macro babel-plugin-macros\n// .babelrc or babel.config.js:\n{\n  \"plugins\": [\"macros\"]\n}\n\n// myData.yaml:\n// - item1\n// - item2: value\n\n// myFile.js:\nimport yaml from 'yaml.macro';\n\nconst myData = yaml('./myData.yaml');\nconsole.log(myData); // ['item1', { item2: 'value' }]\n\nexport default myData;","lang":"javascript","description":"Shows setup with babel-plugin-macros, a YAML file, and importing/using yaml.macro to inline YAML content at build time."},"warnings":[{"fix":"Use a constant string: yaml('./myfile.yaml') not yaml(filePathVariable)","message":"The 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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Assign the result to a variable and reuse it: const data = yaml('./file.yaml'); then use data multiple times.","message":"Multiple calls to the same YAML file are not cached. Each call will re-parse the file, increasing build time.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Upgrade Node.js to version 10 or higher.","message":"Version 0.2.0 requires Node.js >=10. Older versions (0.1.x) may work on Node 8 but are unsupported.","severity":"breaking","affected_versions":">=0.2.0"},{"fix":"Refer to yaml package changelog for breaking changes between versions.","message":"The macro uses the 'yaml' package version 2 internally. If you rely on specific parsing behavior from older versions, check compatibility.","severity":"deprecated","affected_versions":">=0.2.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use import yaml from 'yaml.macro'","cause":"Attempting to use require() or named import instead of default import.","error":"yaml.macro is not a function"},{"fix":"npm install --save-dev babel-plugin-macros","cause":"Missing babel-plugin-macros package or not installed as dev dependency.","error":"Cannot find module 'babel-plugin-macros'"},{"fix":"yaml('./your-file.yaml') — path must be a string literal","cause":"Passing a variable or expression as the path argument instead of a string literal.","error":"The macro threw an error: 'path' is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}