Registry / serialization / semver-parser

semver-parser

JSON →
library4.1.8jsnpmunverified

semver-parser is a JavaScript/TypeScript library designed for robust parsing, verification, and comparison of Semantic Versioning (SemVer) strings. The current stable version is 4.1.8. The package maintains a stable release cadence, with recent updates primarily focusing on tooling and maintenance rather than introducing breaking changes or new features. A key differentiator of `semver-parser` is its developer-friendly approach to 'v' prefixes; it accepts version strings like 'v1.2.3' by default, while also offering a `strict` parameter to enforce strict SemVer adherence, rejecting such prefixes. It provides both synchronous and asynchronous (Promise-based) APIs for its core functionalities, accessible through distinct import patterns, offering flexibility for various application environments and coding styles. It ships with TypeScript types for enhanced developer experience.

npm install semver-parser
INSTALL
IMPORT
SIG · SEMVER-PARSER
S
semver-parser
serializationjavascriptv4.1.8
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parseSemVer
import { parseSemVer } from 'semver-parser'
const parseSemVer = require('semver-parser').parseSemVer
This is for the synchronous parsing function. The package is primarily designed for ES Module imports. CommonJS `require` may result in `ERR_REQUIRE_ESM`.
isValidSemVer
import { isValidSemVer } from 'semver-parser'
import isValidSemVer from 'semver-parser'
All primary APIs are named exports. There is no default export.
promises
import { promises } from 'semver-parser'; const { parseSemVer, isValidSemVer, compareSemVer } = promises;
import { parseSemVer } from 'semver-parser/promises'
Asynchronous (Promise-based) versions of the functions are nested within the `promises` object, which is a named export from the main package.

Demonstrates both synchronous and asynchronous parsing, validation, and comparison of SemVer strings, including the use of the `strict` option.

import { parseSemVer, isValidSemVer, compareSemVer, promises } from 'semver-parser'; async function runSemVerExamples() { const versionString = 'v1.2.3-alpha.1+build.123'; const baseVersionString = '1.2.0'; const strictVersion = '1.0.0'; console.log('--- Synchronous API Examples ---'); // Parse a version string (accepts 'v' prefix by default) const parsed = parseSemVer(versionString); console.log(`Parsed '${versionString}':`, JSON.stringify(parsed)); // Expected: { version: 'v1.2.3-alpha.1+build.123', matches: true, major: 1, minor: 2, patch: 3, pre: [ 'alpha', 1 ], build: [ 'build', 123 ] } // Parse strictly (rejects 'v' prefix) const parsedStrict = parseSemVer(versionString, true); console.log(`Parsed strictly '${versionString}':`, JSON.stringify(parsedStrict)); // Expected: { version: 'v1.2.3-alpha.1+build.123', matches: false } // Validate a version string const isValid = isValidSemVer(strictVersion); console.log(`Is '${strictVersion}' valid SemVer?`, isValid); // true const isValidVPrefixStrict = isValidSemVer('v1.0.0', true); console.log(`Is 'v1.0.0' valid SemVer (strict)?`, isValidVPrefixStrict); // false // Compare two version strings const comparisonResult = compareSemVer(versionString, baseVersionString); console.log(`Compare '${versionString}' vs '${baseVersionString}':`, comparisonResult); // Expected: 1 (versionString is greater) console.log('\n--- Asynchronous API Examples ---'); // Destructure async functions from the 'promises' object const { parseSemVer: parseSemVerAsync, isValidSemVer: isValidSemVerAsync, compareSemVer: compareSemVerAsync } = promises; const parsedAsync = await parseSemVerAsync(versionString); console.log(`Parsed (async) '${versionString}':`, JSON.stringify(parsedAsync)); const isValidAsync = await isValidSemVerAsync(strictVersion); console.log(`Is '${strictVersion}' valid SemVer (async)?`, isValidAsync); const comparisonAsync = await compareSemVerAsync(versionString, baseVersionString); console.log(`Compare (async) '${versionString}' vs '${baseVersionString}':`, comparisonAsync); } runSemVerExamples().catch(console.error);
Debug
Known issues
gotchaBy default, `semver-parser` accepts version strings prefixed with a 'v' (e.g., 'v1.2.3') as valid. While common, this is not strictly compliant with the SemVer 2.0.0 specification.
fix
To enforce strict SemVer compliance and reject 'v' prefixes, pass `true` as the second argument to `parseSemVer` or `isValidSemVer`, e.g., `parseSemVer(version, true)`.
affects: >=1.0.0
gotchaThe asynchronous versions of the core functions (`parseSemVer`, `isValidSemVer`, `compareSemVer`) are not directly exported. They are nested within a `promises` object.
fix
Access asynchronous functions by importing the `promises` object and then destructuring them: `import { promises } from 'semver-parser'; const { parseSemVer, isValidSemVer } = promises;`
affects: >=1.0.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM: require() of ES Module [path]/node_modules/semver-parser/dist/index.js from [path]/your-project/index.js not supported.
Attempting to import `semver-parser` using CommonJS `require()` syntax in a Node.js environment, but the package is published as an ES Module.
fix
Ensure your project uses ES Module `import` syntax (`import { parseSemVer } from 'semver-parser';`) and your environment is configured for ESM (e.g., `"type": "module"` in `package.json` for Node.js, or a bundler configured for ESM).
TypeError: Cannot read properties of undefined (reading 'parseSemVer')
This error typically occurs when trying to access asynchronous functions like `parseSemVer` directly from the main `semver-parser` import, or if the `promises` object was not correctly imported/destructured.
fix
For asynchronous operations, correctly import and destructure from the `promises` object: `import { promises } from 'semver-parser'; const { parseSemVer } = promises;` For synchronous operations, ensure you are importing the synchronous function directly: `import { parseSemVer } from 'semver-parser';`
Upgrade
Version history
4.1.8latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
semver-parser — npm install semver-parser · libregistry