npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@magic/semver

v0.0.20

Published

semantic version parsing and serialization.

Readme

@magic/semver

semantic version parsing and stringification

html-docs

NPM version Linux Build Status Windows Build Status Coverage Status Known Vulnerabilities

installation:

npm install @magic/semver

usage:

import semver from '@magic/semver'

// -alpha.0 is optional.
const version = '0.0.1-alpha.0'

const parsed = semver.parse(version)

// parsed === { major: 0, minor: 0, patch: 1, demo: { string: 'alpha', version: 0 }, v: '0.0.1-alpha.0' }

const serialized = semver.serialize(parsed)

// version === serialized

API

parse(version)

Parses a semver string into a VersionResult object.

Parameters:

  • version (string): A semantic version string (e.g., "1.2.3", "1.2.3-alpha.0", "v1.2.3")

Returns: VersionResult — An object with major, minor, patch, demo, and v properties.

semver.parse('1.2.3')
// { major: 1, minor: 2, patch: 3, v: '1.2.3' }

semver.parse('1.2.3-alpha.0')
// { major: 1, minor: 2, patch: 3, demo: { string: 'alpha', version: 0 }, v: '1.2.3-alpha.0' }

semver.parse('v1.2.3')
// { major: 1, minor: 2, patch: 3, v: 'v1.2.3' }

serialize(version)

Aliases: stringify

Converts a Version object back into a semver string.

Parameters:

  • version (Version): An object with major, minor, and patch properties. Optional demo property for pre-release versions.

Returns: (string): The serialized semver string.

semver.serialize({ major: 1, minor: 2, patch: 3 })
// '1.2.3'

semver.serialize({ major: 1, minor: 2, patch: 3, demo: { string: 'alpha', version: 0 } })
// '1.2.3-alpha.0'

isSemver(value)

Type guard that checks if a value is a valid semver (either a string or object).

Parameters:

  • value (unknown): The value to check.

Returns: (boolean): true if the value is a valid semver string or object.

semver.isSemver('1.2.3') // true
semver.isSemver('1.2.3-alpha.0') // true
semver.isSemver({ major: 1, minor: 2, patch: 3 }) // true
semver.isSemver('not-a-version') // false
semver.isSemver(123) // false

bump(version, options)

Increments a semver version. Returns a string if input was a string, or a Version object if input was an object.

Parameters:

  • version (string | Version): The version to bump.
  • options (object):
    • options.major (boolean): Bump major version, resets minor and patch.
    • options.minor (boolean): Bump minor version, resets patch.
    • options.patch (boolean): Bump patch version.
    • options.alpha (boolean): Set or increment alpha pre-release.
    • options.beta (boolean): Set or increment beta pre-release.

Returns: (string | Version): The bumped version in the same format as input.

semver.bump('1.2.3', { major: true }) // '2.0.0'
semver.bump('1.2.3', { minor: true }) // '1.3.0'
semver.bump('1.2.3', { patch: true }) // '1.2.4'
semver.bump('1.2.3', { alpha: true }) // '1.2.4-alpha.0'
semver.bump('1.2.3-alpha.0', { alpha: true }) // '1.2.3-alpha.1'
semver.bump('1.2.3-alpha.0', { beta: true }) // '1.2.3-beta.0'
semver.bump('1.2.3', { beta: true }) // '1.2.4-beta.0'

// No options: increments alpha version if present, otherwise patch
semver.bump('1.2.3-alpha.0') // '1.2.3-alpha.1'
semver.bump('1.2.3') // '1.2.4'

isBigger(a, b)

Compares two versions. Returns true if a is greater than b.

Parameters:

  • a (string): First version string.
  • b (string): Second version string.

Returns: (boolean): true if a > b.

semver.isBigger('2.0.0', '1.0.0') // true
semver.isBigger('1.0.0', '1.0.0') // false
semver.isBigger('1.0.0-alpha', '1.0.0') // false (release > pre-release)
semver.isBigger('1.0.0-beta', '1.0.0-alpha') // true (beta > alpha)

isSmaller(a, b)

Compares two versions. Returns true if a is less than b.

Parameters:

  • a (string): First version string.
  • b (string): Second version string.

Returns: (boolean): true if a < b.

semver.isSmaller('1.0.0', '2.0.0') // true
semver.isSmaller('1.0.0', '1.0.0') // false
semver.isSmaller('1.0.0-alpha', '1.0.0') // true (pre-release < release)

sort(versions)

Sorts an array of versions in ascending order according to semver spec (pre-releases come before releases, alpha < beta).

Parameters:

  • versions (Array<string | Version>): An array of version strings or objects.

Returns: (Array<string | Version>): The sorted array, preserving input types.

semver.sort(['1.0.0', '2.0.0-alpha', '1.0.0-alpha.0', '2.0.0'])
// ['1.0.0-alpha.0', '1.0.0', '2.0.0-alpha', '2.0.0']

semver.sort(['1.2.3', '1.2.0', '1.3.0'])
// ['1.2.0', '1.2.3', '1.3.0']

Types

type Version = {
  major: number
  minor: number
  patch: number
  demo?: {
    string: string // 'alpha' or 'beta'
    version: number
  }
}

type VersionResult = Version & { v: string }
changelog
0.0.1

first release

0.0.2

add bump and isSemver

0.0.3

handle -alpha and -beta separately to stop decrementing the version by mistake.

0.0.4

fix behaviour of appending -alpha.0 and -beta.0. these two edgecases also bump patch version now.

0.0.5

add isBigger and isSmaller version comparison

0.0.6

remove @magic/fs

0.0.7

bump required node version to 14.2.0

0.0.8

update dependencies

0.0.9
  • bump required node version to 14.15.4
  • update dependencies
0.0.10
  • update README
  • bump dev dependencies
0.0.11

update dependencies

0.0.12

update dependencies

0.0.13

update dependencies

0.0.14

update dependencies

0.0.15

update dependencies

0.0.16

update dependencies

0.0.17
  • add typescript
  • update dependencies
0.0.18
  • fix handling a v in front of the version, eg v0.0.1
0.0.19
  • better type exports
0.0.20
  • add sort function, sorts arrays of versions
  • add documentation to README.md
  • update dependencies
0.0.21 - unreleased

...