vfile-matter
vfile-matter is a utility package for the vfile ecosystem, designed to parse YAML front matter found at the beginning of a virtual file. It's currently stable at version 5.0.1, with minor updates and patch releases occurring as needed, and major versions released periodically to align with ecosystem updates and Node.js LTS cycles. This package populates the parsed data into `file.data.matter` and can optionally strip the front matter content from the file's `value`. A key differentiator is its tight integration with `vfile`, making it suitable for direct file manipulation when not using a higher-level parsing system like remark. Since version 4.0.0, it uses the more modern `yaml` package for parsing, replacing `js-yaml`. It's important to note that this package is distinct from `remark-frontmatter`, which should be used when processing Markdown with `remark` rather than directly manipulating `vfile` objects.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to `require()` vfile-matter in a CommonJS module, but it is an ESM-only package since v3.0.0.fixChange `const { matter } = require('vfile-matter')` to `import { matter } from 'vfile-matter'` and ensure your project uses ES modules (e.g., `"type": "module"` in package.json or `.mjs` file extension). -
TypeError: matter is not a function
cause Incorrect import statement (e.g., `import matter from 'vfile-matter'` instead of named import) or calling `matter` on an undefined/null value.fixEnsure you are using a named import: `import { matter } from 'vfile-matter'`. Also verify that the `file` object passed to `matter` is a valid `VFile` instance. -
Cannot find module 'vfile-matter'
cause The package is not installed, or Node.js version is incompatible with the `exports` map defined in `package.json` (since v5.0.0), or there's a problem with module resolution.fixFirst, ensure `npm install vfile-matter` is run. If it persists, check your Node.js version (must be 16+ for v5.0.0+). If using specific module resolution tools, check their configuration.
Warnings
- breaking vfile-matter v5.0.0 and above require Node.js 16 or higher. Running on older Node.js versions will result in errors.
- breaking Since v5.0.0, the `matter` function no longer returns the `VFile` object, but instead returns `undefined`. Operations are performed directly on the `file` object passed as an argument.
- breaking vfile-matter switched to ESM-only in v3.0.0. CommonJS `require()` statements will fail with `ERR_REQUIRE_ESM`.
- breaking vfile-matter v4.0.0 replaced the `js-yaml` parser with the `yaml` package. While largely API compatible, specific options or behaviors tied to `js-yaml` might change.
- gotcha If you are processing Markdown files with `remark`, it is generally recommended to use `remark-frontmatter` instead of `vfile-matter`. `remark-frontmatter` integrates directly into the AST processing, preserving positional information correctly.
- gotcha Using the `strip: true` option to remove front matter from the file's content can alter positional information. This can cause issues with other tools or plugins that rely on accurate character offsets for warnings, errors, or source map generation.
Install
-
npm install vfile-matter -
yarn add vfile-matter -
pnpm add vfile-matter
Imports
- matter
const matter = require('vfile-matter')import { matter } from 'vfile-matter' - Options
import { Options } from 'vfile-matter'import type { Options } from 'vfile-matter' - YamlOptions
import { YamlOptions } from 'vfile-matter'import type { YamlOptions } from 'vfile-matter'
Quickstart
import { read } from 'to-vfile';
import { matter } from 'vfile-matter';
const exampleFileContent = `---\nlayout: solar-system\ntitle: Jupiter\n---\n<h1>Jupiter</h1>\n`;
async function processFile() {
// In a real scenario, you'd read from a file path
// For this example, we create a VFile directly
const file = await read({ path: 'example.html', value: exampleFileContent });
// Parse the YAML front matter and optionally strip it
matter(file, { strip: true });
console.log('Parsed Matter:', file.data.matter);
console.log('File Content (after stripping):', String(file));
}
processFile().catch(console.error);
/*
To run this example, ensure you have 'vfile-matter' and 'to-vfile' installed:
npm install vfile-matter to-vfile
*/