Core version comparison functionality providing both numeric and boolean comparison modes. Supports the full semver specification with flexible format handling.
Numeric comparison of version strings returning values compatible with Array.sort().
/**
* Compare semver version strings to find greater, equal or lesser.
* This library supports the full semver specification, including comparing versions
* with different number of digits like `1.0.0`, `1.0`, `1`, and pre-release versions like `1.0.0-alpha`.
* @param v1 - First version to compare
* @param v2 - Second version to compare
* @returns Numeric value compatible with the Array.sort(fn) interface.
* Returns 1 if v1 > v2, 0 if v1 === v2, -1 if v1 < v2
* @throws TypeError if either argument is not a string
* @throws Error if version string is not valid semver format
*/
function compareVersions(v1: string, v2: string): number;Usage Examples:
import { compareVersions } from "compare-versions";
// Basic comparison
compareVersions('1.0.0', '2.0.0'); // -1 (first is smaller)
compareVersions('2.0.0', '1.0.0'); // 1 (first is larger)
compareVersions('1.0.0', '1.0.0'); // 0 (equal)
// Pre-release versions
compareVersions('1.0.0-alpha', '1.0.0'); // -1 (pre-release < release)
compareVersions('1.0.0-alpha', '1.0.0-beta'); // -1 (alpha < beta lexically)
compareVersions('1.0.0-alpha.1', '1.0.0-alpha.2'); // -1
// Different digit counts
compareVersions('1.0', '1.0.0'); // 0 (equivalent)
compareVersions('1', '1.0.0'); // 0 (equivalent)
compareVersions('1.0.1', '1.0'); // 1 (1.0.1 > 1.0.0)
// Sorting with Array.sort()
const versions = ['1.5.19', '1.2.3', '1.5.5', '2.0.0'];
versions.sort(compareVersions);
// Result: ['1.2.3', '1.5.5', '1.5.19', '2.0.0']
// Reverse sorting
versions.sort((a, b) => compareVersions(b, a));
// Result: ['2.0.0', '1.5.19', '1.5.5', '1.2.3']Error Handling:
The compareVersions function validates inputs and throws descriptive errors:
// These will throw errors
compareVersions(123, '1.0.0'); // TypeError: Invalid argument expected string
compareVersions('1.0.0', undefined); // TypeError: Invalid argument expected string
compareVersions('invalid', '1.0.0'); // Error: Invalid argument not valid semver ('invalid' received)
compareVersions('1.0.0', '6.3.'); // Error: Invalid argument not valid semver ('6.3.' received)Human-readable comparison using arithmetic operators.
/**
* Compare semver version strings using the specified operator.
* @param v1 First version to compare
* @param v2 Second version to compare
* @param operator Allowed arithmetic operator to use
* @returns `true` if the comparison between the firstVersion and the secondVersion satisfies the operator, `false` otherwise.
* @throws TypeError if operator is not a string
* @throws Error if operator is not one of: >, >=, =, <=, <, !=
*/
function compare(v1: string, v2: string, operator: CompareOperator): boolean;
type CompareOperator = '>' | '>=' | '=' | '<' | '<=' | '!=';Usage Examples:
import { compare } from "compare-versions";
// Greater than
compare('10.1.8', '10.0.4', '>'); // true
compare('10.0.4', '10.1.8', '>'); // false
// Equal
compare('10.0.1', '10.0.1', '='); // true
compare('1.0', '1.0.0', '='); // true
// Less than or equal
compare('10.1.1', '10.2.2', '<='); // true
compare('10.2.2', '10.1.1', '<='); // false
// Not equal
compare('1.0.0', '1.0.1', '!='); // true
compare('1.0.0', '1.0.0', '!='); // false
// With pre-release versions
compare('1.0.0-alpha', '1.0.0', '<'); // true
compare('1.0.0-beta', '1.0.0-alpha', '>'); // trueThe compare function validates input operators and throws descriptive errors:
// These will throw errors
compare('1.0.0', '2.0.0', 123); // TypeError: Invalid operator type, expected string but got number
compare('1.0.0', '2.0.0', 'invalid'); // Error: Invalid operator, expected one of >|>=|=|<=|<|!=Both functions support all the flexible version formats:
1.0.0, 2.1.3-beta.11.0, 1 (treated as 1.0.0)1.0.x, 1.0.* (in version strings)25.0.1364.126v1.0.0 (v is ignored)1.01.1 becomes 1.1.1Pre-release versions are always considered less than their corresponding release versions, and pre-release identifiers are compared lexically when the main version numbers are equal.