Skip to content

JS API

Three entry points parse commits; all accept the same options:

import {
CommitParser,
parseCommits,
parseCommitsStream
} from 'conventional-commits-parser'

See Introduction for a first example.

new CommitParser(options?) · parser.parse(rawCommit)

Section titled “new CommitParser(options?) · parser.parse(rawCommit)”

Parse a single raw commit message synchronously. Throws if the message can’t be parsed:

const parser = new CommitParser(options)
const commit = parser.parse('feat(api): add async write() generator')

Returns an async-generator function that parses an iterable of raw commits — for stream.pipeline:

import { pipeline } from 'node:stream/promises'
await pipeline(
rawCommits,
parseCommits(options),
async function* (commits) {
for await (const commit of commits) {
console.log(commit)
}
}
)

Returns a Node.js Transform that turns raw commits into parsed ones:

rawCommitsStream
.pipe(parseCommitsStream(options))
.on('data', commit => console.log(commit))

parse() produces an object with these fields:

FieldTypeDescription
type, scope, subjectstring | nullHeader parts, named by headerCorrespondence.
headerstring | nullThe first line of the message.
bodystring | nullThe commit body.
footerstring | nullThe footer section.
mergestring | nullThe matched merge header, if any.
notes{ title, text }[]Notes such as breaking changes.
mentionsstring[]@-mentions found in the message.
referencesCommitReference[]Issue references (see below).
revertRecord<string, string | null> | nullReverted-commit fields, when the commit is a revert.

Each entry in references describes an issue reference:

FieldDescription
rawThe raw reference text, e.g. #42.
actionThe keyword that referenced it, e.g. Closes (or null).
ownerReferenced owner, or null.
repositoryReferenced repository, or null.
issueThe issue number.
prefixThe issue prefix, e.g. #.

The same options configure all three functions. Defaults come from the defaultOptions:

OptionTypeDefaultDescription
headerPatternRegExp/^(\w*)(?:\(([\w$@.\-*/ ]*)\))?: (.*)$/Matches the header into capture groups.
headerCorrespondencestring[]['type', 'scope', 'subject']Names for the header pattern’s capture groups.
breakingHeaderPatternRegExpMatches a breaking-change header (e.g. feat!: …).
mergePatternRegExpMatches merge / PR headers; the next line is used as the conventional header.
mergeCorrespondencestring[]Names for the merge pattern’s capture groups.
revertPatternRegExp/^Revert\s"([\s\S]*)"\s*This reverts commit (\w*)\.?/Detects revert commits.
revertCorrespondencestring[]['header', 'hash']Names for the revert pattern’s capture groups.
fieldPatternRegExp/^-(.*?)-$/Matches custom -field- blocks in the body.
noteKeywords(string | RegExp)[]['BREAKING CHANGE', 'BREAKING-CHANGE']Keywords that start a note. Case-insensitive.
notesPattern(text) => RegExpBuilds the note-matching regex from the note keywords.
issuePrefixes(string | RegExp)[]['#']Prefixes that mark an issue reference.
issuePrefixesCaseSensitivebooleanfalseTreat issuePrefixes as case-sensitive.
referenceActions(string | RegExp)[]close, closes, closed, fix, fixes, fixed, resolve, resolves, resolvedKeywords that reference an issue. Case-insensitive.
commentCharstringCharacter that comments out a line.
warnboolean | (message) => voidnoopparseCommits / parseCommitsStream only. Handles parse errors; true throws (strict mode).

Recognize a custom issue prefix and reference keyword:

import { CommitParser } from 'conventional-commits-parser'
const parser = new CommitParser({
issuePrefixes: ['#', 'GH-'],
referenceActions: ['closes', 'fixes', 'refs']
})
parser.parse('fix(api): handle timeouts\n\nrefs GH-17')