SemVer (Semantic Versioning) JavaScript library's
TypeScript
Tree-Shakable
Tiny Size
Turbo Speed[1]
Version
A fork and drop-in replacement of npm's official semver library, it has all 26 functions of the original library (except satisfies
)[2], with the same implementation and the same API, passes all the original's tests[3], but:
Installation:
npm install semver-ts
In your ES module (recommanded instead of CommonJS) JavaScript/TypeScript code:
import { compare } from "semver-ts"
compare("1.0.0", "1.0.3")
Or directly in HTML (not tree-shakable, obviously):
<script src="https://unpkg.com/semver-ts/dist/index.global.js"></script>
<script>semver.compare("1.0.0", "1.0.3")</script>
Documentation: https://semver.tomchen.org/
ES module treeshaked and minified (with terser) bundle size comparison:
Code | Minified | Gzipped |
---|---|---|
import { compare } from "semver-ts" compare("1.0.0", "1.0.3") |
2.00 KB | 914 bytes |
import compare from "semver/functions/compare" compare("1.0.0", "1.0.3") |
8.95 KB | 2.80 KB |
import { compare } from "semver" compare("1.0.0", "1.0.3") |
25.9 KB | 7.55 KB |
[1] Turbo Speed: OK, the current version faithfully uses the original implementation so it's not really that fast, but it should be slightly faster than the original due to the fact that it removes the class-based structure and uses a purely functional approach
[2] All 'semver/functions/*', except for 'semver/functions/satisfies' which is intended to be included in the future. List of functions: clean
, cmp
, coerce
, compare
, compareBuild
, compareCore
, compareIdentifiers
, compareLoose
, diff
, eq
, gt
, gte
, inc
, incThrow
, lt
, lte
, major
, minor
, neq
, parse
, patch
, prerelease
, rcompare
, rsort
, sort
, valid
[3] Faithfully uses the same implementation code, same function signatures (even the same weird overload of inc()
) (only replacing SemVer class by an object containing parsed semver information), and passed all the tests in the original library (semver v7.7.1 (2025-02-03))
[4] Not tree-shakable with CommonJS exports. The support for const a = require("semver/a")
CommonJS treeshakability could be done but will increase the size of the ESM bundle. So, just use the modern ES modules
[5] Currently, class is not tree-shakable with any bundler, and with either ES modules or CommonJS exports. Therefore, despite the effort to make the original semver tree-shakable, user's bundling output may still contain a lot of unused code, mainly from the SemVer class
[6] Some functions in @types/semver, such as inc()
, do not seem to have correct types
[7] You can hover over a function to see very detailed information including examples in VS Code thanks to the comments. The documentation webpages are automatically generated from the comments with TypeDoc.