Semantic Version Comparison Utility
This package provides a lightweight utility function for comparing two semantic version strings, adhering to the semver specification. It evaluates major, minor, patch, and pre-release identifiers, returning -1 if the first version is less than the second, 0 if they are equal, and 1 if the first is greater. The current stable version is 1.0.3, which was last published over six years ago (as of 2026), indicating that the project is largely abandoned and no longer receives active maintenance or updates. Its key differentiator is its singular focus on version comparison without supporting more complex features like version ranges. Due to its age and lack of updates, it exclusively uses the CommonJS module format. Newer projects or those requiring ESM compatibility, TypeScript support, or active development should consider more modern alternatives.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to `require()` an ESM module in a CommonJS context, or more commonly, trying to `import` this CJS-only package in a Node.js project configured for ESM without proper interoperability handling.fixEnsure your Node.js project is configured for CommonJS, or use a bundler that transpiles CJS modules for ESM consumption. For direct Node.js ESM, this package is not compatible. -
TypeError: compare is not a function
cause Incorrectly importing the default export as a named export (e.g., `import { compare } from 'pkg';`) or attempting to call `require('pkg').compare`.fixThe package exports a single function as its default. In CommonJS, use `const compare = require('node-version-compare');`. If using ESM with a bundler, `import compare from 'node-version-compare';` is the correct pattern.
Warnings
- breaking The package is effectively abandoned, with its last publish over six years ago. This means no new features, bug fixes, or security patches will be released.
- gotcha This library is CommonJS (CJS) only. Attempting to use `import` syntax in a pure ESM Node.js environment will result in an error unless a bundler handles the CJS-to-ESM conversion.
- gotcha The package does not ship with TypeScript type definitions, leading to potential type-related issues or requiring manual declaration files (`.d.ts`) when used in TypeScript projects.
- gotcha Given its age, the library may not fully conform to the latest semver specification updates or might have undiscovered edge case parsing bugs that will not be addressed.
Install
-
npm install node-version-compare -
yarn add node-version-compare -
pnpm add node-version-compare
Imports
- compare
const compare = require('node-version-compare'); - compare
import { compare } from 'node-version-compare';import compare from 'node-version-compare';
Quickstart
const compare = require('node-version-compare');
console.log(compare('1.0.0', '1.0.1')); // Expected: -1
console.log(compare('10.2.0-alpha.1', '10.2.0-alpha.1')); // Expected: 0
console.log(compare('10.2.0-beta', '10.2.0-alpha')); // Expected: 1
console.log(compare('3.1.1-1', '3.1.1-2')); // Expected: -1
console.log(compare('1.2.3', '1.2.3-alpha')); // Expected: 1 (stable is greater than pre-release)
console.log(compare('1.0.0-rc.1', '1.0.0-rc.1.1')); // Expected: -1