{"id":11996,"library":"semiver","title":"SemiVer String Comparator","description":"SemiVer is a lightweight (187B) utility designed for accurate comparison of semantic version strings, including those with pre-release identifiers (e.g., `1.0.0-alpha.1` vs `1.0.0-beta`). It leverages the native JavaScript `Intl.Collator` API to perform language-sensitive string comparisons, ensuring correctness across various versioning formats. The library is currently stable at version 1.1.0 and is actively maintained with an as-needed release cadence for bug fixes and minor enhancements. Its primary differentiators are its minimal bundle size, its reliance on a native Web API rather than a heavy parsing library, and its direct compatibility with `Array.sort()` as a comparison function, making it an efficient choice for sorting version lists without external dependencies. Unlike other semver utilities that provide parsing and range matching, SemiVer focuses solely on providing a three-way comparison, outputting `0`, `1`, or `-1`.","status":"active","version":"1.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/lukeed/semiver","tags":["javascript","compare","semver","sort","typescript"],"install":[{"cmd":"npm install semiver","lang":"bash","label":"npm"},{"cmd":"yarn add semiver","lang":"bash","label":"yarn"},{"cmd":"pnpm add semiver","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"`semiver` is a default export. Attempting to destructure it as a named export will result in `undefined`.","wrong":"import { semiver } from 'semiver';","symbol":"semiver","correct":"import semiver from 'semiver';"},{"note":"The package provides a CommonJS build, so `require` is fully supported in Node.js environments.","symbol":"semiver","correct":"const semiver = require('semiver');"},{"note":"When using TypeScript, you can import the type of the default export using `import type` for type-only imports.","symbol":"SemiVerFunction","correct":"import type semiver from 'semiver';"}],"quickstart":{"code":"import semiver from 'semiver';\n\nconst versions = [\n  '4.11.6', '4.2.0',\n  '1.5.19', '1.5.5',\n  '1.0.0', '1.0.0-rc.1',\n  '1.2.3', '1.2.3-alpha',\n  '1.0.0-alpha.1', '1.0.0-alpha',\n  '1.0.0-beta.11', '1.0.0-beta'\n];\n\n// Sort an array of semver strings using semiver directly\nversions.sort(semiver);\n\nconsole.log(versions);\n/* Expected output:\n[\n  '1.0.0-alpha',\n  '1.0.0-alpha.1',\n  '1.0.0-beta',\n  '1.0.0-beta.11',\n  '1.0.0-rc.1',\n  '1.0.0',\n  '1.2.3-alpha',\n  '1.2.3',\n  '1.5.5',\n  '1.5.19',\n  '4.2.0',\n  '4.11.6'\n]\n*/","lang":"typescript","description":"Demonstrates how `semiver` can be used directly as a comparison function for `Array.sort()` to sort an array of semantic version strings, including pre-release identifiers, in ascending order."},"warnings":[{"fix":"Ensure the target environment supports `Intl.Collator`. For extremely old environments, consider polyfills, though this is rarely necessary for current usage.","message":"SemiVer relies on the native `Intl.Collator` API for string comparison. While widely supported in modern browsers and Node.js environments (generally since 2017), very old or highly constrained JavaScript runtimes may lack full support or exhibit inconsistent behavior.","severity":"gotcha","affected_versions":"<1.0.0 (where support was less prevalent, though the package itself is recent)"},{"fix":"Always ensure that input strings are valid semantic versions to guarantee accurate comparisons.","message":"SemiVer is designed to compare strings that adhere to the Semantic Versioning 2.0.0 specification. Providing malformed or non-compliant version strings may lead to unexpected or incorrect sorting results, as the underlying `Intl.Collator` will perform a standard string comparison rather than a semver-aware one for invalid inputs.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Update your Node.js version (Node.js 6+ includes `Intl.Collator`) or ensure your browser environment is modern enough (Chrome 24+, Firefox 29+, Edge 12+, Safari 10+).","cause":"The JavaScript runtime environment does not provide the global `Intl` object or `Intl.Collator` constructor.","error":"ReferenceError: Intl is not defined"},{"fix":"Verify that `semiver` is correctly imported via `import semiver from 'semiver';` (ESM) or `const semiver = require('semiver');` (CJS) and then passed directly to `Array.sort()` like `arr.sort(semiver)`.","cause":"The `semiver` function was not correctly imported or passed to `Array.sort()`, or a non-function value was provided.","error":"TypeError: The comparison function must be either a function or undefined"}],"ecosystem":"npm"}