vite-plugin-yaml2
raw JSON → 1.1.5 verified Mon Apr 27 auth: no javascript
Vite plugin to import YAML files as JavaScript default exports. Current stable version is 1.1.5, released with basic YAML loading support. The plugin registers a Vite plugin that transforms .yaml and .yml files into JS objects via default export. It is a lightweight alternative to vite-plugin-yaml (which may have different export behavior) and does not require additional loaders. Release cadence appears low; the package has had only a few bug fix releases since v1.0.0. The plugin is minimal, with no configuration options beyond the plugin call.
Common errors
error Cannot find module 'vite-plugin-yaml2' or its corresponding type declarations. ↓
cause Missing type declarations for the plugin or the imported YAML files.
fix
Ensure vite-plugin-yaml2 is installed: npm i vite-plugin-yaml2. For YAML files, add type declarations as shown in the quickstart warnings.
error Uncaught SyntaxError: The requested module 'vite-plugin-yaml2' does not provide an export named 'pluginYaml' ↓
cause Used named import instead of default import.
fix
Use import pluginYaml from 'vite-plugin-yaml2' instead of import { pluginYaml } from ...
error Error: Cannot find module 'yaml' ↓
cause The dependency 'yaml' is not installed.
fix
Install yaml: npm i yaml (or it should be automatically installed as a dependency of vite-plugin-yaml2, but manual install may fix).
error TypeScript error: Cannot find module './data.yml' or its corresponding type declarations. ↓
cause No ambient module declaration for .yml/.yaml files.
fix
Add declare module '*.yaml' { const value: any; export default value; } to src/vite-env.d.ts.
Warnings
gotcha Types for imported YAML files are not provided; you must manually add a module declaration. ↓
fix Add declare module '*.yaml' { const value: any; export default value; } in a .d.ts file.
deprecated No known deprecations, but the package has seen few updates; may not follow Vite's latest plugin API changes. ↓
fix If issues arise with newer Vite versions, consider using vite-plugin-yaml (alternative) or file an issue.
gotcha The plugin uses the yaml package to parse; deeply nested structures may throw if YAML is invalid. ↓
fix Validate YAML syntax before importing.
Install
npm install vite-plugin-yaml2 yarn add vite-plugin-yaml2 pnpm add vite-plugin-yaml2 Imports
- pluginYaml wrong
import { pluginYaml } from 'vite-plugin-yaml2'correctimport pluginYaml from 'vite-plugin-yaml2' - pluginYaml
const pluginYaml = require('vite-plugin-yaml2')
Quickstart
// vite.config.ts
import { defineConfig } from 'vite';
import pluginYaml from 'vite-plugin-yaml2';
export default defineConfig({
plugins: [pluginYaml()],
});
// Then in any component:
import data from './data.yml';
console.log(data);
// data.yml content:
// - name: Alice
// age: 30
// - name: Bob
// age: 25