parser-front-matter

raw JSON →
1.6.4 verified Sat Apr 25 auth: no javascript maintenance

Front matter parsing middleware based on gray-matter, providing a callback-oriented API for parsing YAML-like front matter from strings or file objects. Version 1.6.4 is the latest stable release, with no major updates since 2019. It supports both async (parse) and sync (parseSync) methods, and is designed for use with build tools like assemble, verb, or generate. Unlike direct gray-matter usage, this package integrates via middleware conventions, but it is slow, unmaintained, and lacks TypeScript definitions.

error Cannot find module 'parser-front-matter'
cause Package is not installed or spelling mistake.
fix
Run npm install parser-front-matter --save
error TypeError: parser.parse is not a function
cause Importing default as an object without destructuring or using wrong import syntax.
fix
Use import parser from 'parser-front-matter' then parser.parse() or import { parse } from 'parser-front-matter'.
error gray-matter is not defined
cause Missing dependency 'gray-matter' (should be installed automatically but may be missing in some environments).
fix
Run npm install gray-matter
gotcha Package is not actively maintained; last release 2019. May not work with modern Node.js versions (>=14).
fix Consider using gray-matter directly or a maintained alternative.
breaking ESM usage may fail in Node.js <12 or with certain bundlers due to lack of 'type: module' in package.json.
fix Use dynamic import() or CommonJS require() for compatibility.
deprecated The parse callback is error-first but does not return a promise. No promise or async/await support.
fix Wrap in a promise: new Promise((resolve, reject) => parser.parse(str, (err, file) => err ? reject(err) : resolve(file))).
npm install parser-front-matter
yarn add parser-front-matter
pnpm add parser-front-matter

Shows async and sync front matter parsing from string and object, with error handling.

import parser from 'parser-front-matter';

// Parse a string with front matter
parser.parse('---\ntitle: Hello\n---\nWorld', (err, file) => {
  if (err) console.error(err);
  console.log(file.data.title); // 'Hello'
  console.log(file.content);     // 'World'
});

// Parse an object (e.g., from gulp)
const fileObj = { contents: Buffer.from('---\nfoo: bar\n---\nbaz') };
parser.parse(fileObj, (err, file) => {
  if (err) console.error(err);
  console.log(file.data.foo); // 'bar'
  console.log(file.content);  // 'baz'
});

// Synchronous usage
const result = parser.parseSync('---\ntitle: Sync\n---\nContent');
console.log(result.data.title); // 'Sync'