unyaml
Universal YAML loader. Works in Node.js (CJS require / ESM import), esbuild and Vite — one package, one mental model.
Node.js
Node.js ^22.15 || >=24 is required.
From CLI
CJS:
node -r unyaml path/to/index.cjs
ESM:
node --import unyaml path/to/index.mjs
Manually import
CJS:
require('unyaml')
require('path/to/file.yml') // now it works!
ESM:
import 'unyaml'
const data = await import('path/to/file.yml')
esbuild
import esbuild from 'esbuild'
import unyaml from 'unyaml/esbuild'
await esbuild.build({
entryPoints: ['src/index.ts'],
bundle: true,
outfile: 'dist/index.js',
plugins: [unyaml()],
})
.yml / .yaml imports are loaded, parsed and inlined as JSON modules.
Options
unyaml({
filter?: RegExp // default: /\.ya?ml$/
// ...plus any js-yaml LoadOptions (schema, json, onWarning, ...)
})
Vite
// vite.config.ts
import { defineConfig } from 'vite'
import unyaml from 'unyaml/vite'
export default defineConfig({
plugins: [unyaml()],
})
The Vite plugin handles both the main pipeline and the esbuild-based dependency pre-bundling (optimizeDeps), so importing YAML from inside node_modules works out of the box.
Options
unyaml({
filter?: RegExp // default: /\.ya?ml(?:\?.*)?$/
// ...plus any js-yaml LoadOptions (schema, json, onWarning, ...)
})
TypeScript support
To silence Cannot find module 'file.yml' or its corresponding type declarations. errors, add unyaml/types to compilerOptions.types in tsconfig.json:
{
"compilerOptions": {
"types": [
"unyaml/types"
]
}
}