Function parse

Return the parsed version of a semver string, if the semver string is not valid, it returns null or throws an error depending on the value of throwErrors

TypeError if the version string is invalid or if there are other errors (if throwErrors is falsy, it will return null instead of throwing an error)

parse('1.2.3') // { version: '1.2.3', fullVersion: "1.2.3", major: 1, minor: 2, patch: 3, prerelease: [], build: [] }
parse('1.2.3-foo.bar+build.123') // { version: '1.2.3-foo.bar', fullVersion: "1.2.3-foo.bar+build.123", major: 1, minor: 2, patch: 3, prerelease: ['foo', 'bar'], build: ['build', '123'] }
parse('1.2') // null
parse('1.2', { loose: true }, true) // throws TypeError: Invalid Version: 1.2

build identifier ([+BUILD]) is taken into account

  • Type Parameters

    • T extends boolean

    Parameters

    • ver: string

      version string

    • OptionaloptionsOrLoose: boolean | Options

      an options object { loose } (where loose is the only available option) or a boolean indicating whether to enable loose mode. In loose mode, the parser is more forgiving with imperfectly formatted semver strings

    • OptionalthrowErrors: T

      whether to throw an error if the version string is invalid or if there are other errors. Defaults to false

    Returns T extends true ? ParsedVersion : null | ParsedVersion

    parsed version object, or null if the version string is invalid or if there are other errors (if throwErrors is truthy, an error will be thrown instead of returning null)