Skip to content

Instantly share code, notes, and snippets.

@anchal20
Last active February 14, 2018 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anchal20/48b5f318fdb441aec10d783506ce7dfb to your computer and use it in GitHub Desktop.
Save anchal20/48b5f318fdb441aec10d783506ce7dfb to your computer and use it in GitHub Desktop.
Explaining the meaning of '~', '^' etc of node package versions in package.json

In the package.json file we see that there are devDependencies and dependencies with some node packages. Something like this - "babel": "^6.26.0"

Now, many of us do not understand what the symbol at the start of the version means. This gist will give you a consolidated solution to the question.

Premitive operators

  • < Less than
  • <= Less than or equal to
  • > Greater than
  • >= Greater than or equal to
  • = Equal (optional and can be omiited, as any version without an operator is assumed to be equal)

Explanation for each operators

  1. < Less than - <1.4.7 would match the versions 1.4.6, 1.4.5 but not 1.4.7 or 1.5.0

  2. <= Less than or equal to - similar to the above but will include 1.4.7

  3. > Greater than - >1.3.8 would match the versions 1.3.9, 1.4.2 but not 1.2.7, 1.3.8 or less

  4. >= Greater than or equal to - same as in point 3 except it will include 1.3.8 as well.

Comparators can be joined by whitespace to form a comparator set, which is satisfied by the intersection of all of the comparators it includes. For example, the range >=1.2.2 <1.3.0 would match the versions 1.2.2, 1.2.3, and 1.2.99, but not the versions 1.2.1, 1.3.0, or 1.1.0 The range 1.2.7 || >=1.2.9 <2.0.0 would match the versions 1.2.7, 1.2.9, and 1.4.6, but not the versions 1.2.8 or 2.0.0.

Hyphen Ranges a.b.c - x.y.z

It inludes the versions inclusive of the boundary of the set

  • 1.2.3 - 1.3.1 ~= >=1.2.3 <=1.3.1

X-Ranges 1.2.x, 1.x, 1.2.*

  • Any of x or * can be replaced with one of the numeric values in the [major, minor, patch] tuple.
  • * := >=0.0.0 (Any version satisfies)
  • 1.x := >=1.0.0 <2.0.0 (Matching major version)
  • 1.2.x := >=1.2.0 <1.3.0 (Matching major and minor versions)
  • 1 := 1.x.x := >=1.0.0 <2.0.0
  • 1.2 := 1.2.x := >=1.2.0 <1.3.0

Tilde Ranges ~1, ~2.3, ~2.4.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment