What is the bower (and npm) version syntax?

Samuel Hapak

Bower enables me to specify version requirements for packages using the following syntax:

"dependencies": {
  "<name>": "<version>",
},

But I have not been able to find what is the syntax to use for the <version>. I know that I can specify versions to be:

  • greater than a certain version with ">1.0.0"
  • greater than or equal to a version: ">=1.0.0"
  • or in some range: "1.0.0 - 2.0.0".

I also know that there is a common version syntax containing the tilde: "~1.0.0". But I am not sure what it means and whether it is the same as "=1.0.0".

I am also interested to know whether I am able to specify multiple non-consecutive versions, such as exactly 1.0.3 plus versions greater than 1.5.0, etc...

XML

In a nutshell, the syntax for Bower version numbers (and NPM's) is called SemVer, which is short for 'Semantic Versioning'. You can find documentation for the detailed syntax of SemVer as used in Bower and NPM on the API for the semver parser within Node/npm. You can learn more about the underlying spec (which does not mention ~ or other syntax details) at semver.org.

There's a super-handy visual semver calculator you can play with, making all of this much easier to grok and test.

SemVer isn't just a syntax! It has some pretty interesting things to say about the right ways to publish API's, which will help to understand what the syntax means. Crucially:

Once you identify your public API, you communicate changes to it with specific increments to your version number. Consider a version format of X.Y.Z (Major.Minor.Patch). Bug fixes not affecting the API increment the patch version, backwards compatible API additions/changes increment the minor version, and backwards incompatible API changes increment the major version.

So, your specific question about ~ relates to that Major.Minor.Patch schema. (As does the related caret operator ^.) You can use ~ to narrow the range of versions you're willing to accept to either:

  • subsequent patch-level changes to the same minor version ("bug fixes not affecting the API"), or:
  • subsequent minor-level changes to the same major version ("backwards compatible API additions/changes")

For example: to indicate you'll take any subsequent patch-level changes on the 1.2.x tree, starting with 1.2.0, but less than 1.3.0, you could use:

"angular": "~1.2"
  or:
"angular": "~1.2.0"

This also gets you the same results as using the .x syntax:

"angular": "1.2.x"

But, you can use the tilde/~ syntax to be even more specific: if you're only willing to accept patch-level changes starting with 1.2.4, but still less than 1.3.0, you'd use:

"angular": "~1.2.4"

Moving left, towards the major version, if you use...

"angular": "~1"

... it's the same as...

"angular": "1.x"
  or:
"angular": "^1.0.0"

...and matches any minor- or patch-level changes above 1.0.0, and less than 2.0:

Note that last variation above: it's called a 'caret range'. The caret looks an awful lot like a >, so you'd be excused for thinking it means "any version greater than 1.0.0". (I've certainly slipped on that.) Nope!

Caret ranges are basically used to say that you care only about the left-most significant digit - usually the major version - and that you'll permit any minor- or patch-level changes that don't affect that left-most digit. Yet, unlike a tilde range that specifies a major version, caret ranges let you specify a precise minor/patch starting point. So, while ^1.0.0 === ~1, a caret range such as ^1.2.3 lets you say you'll take any changes >=1.2.3 && <2.0.0. You couldn't do that with a tilde range.

That all seems confusing at first, when you look at it up-close. But zoom out for a sec, and think about it this way: the caret simply lets you say that you're most concerned about whatever significant digit is left-most. The tilde lets you say you're most concerned about whichever digit is right-most. The rest is detail.

It's the expressive power of the tilde and the caret that explains why people use them much more than the simpler .x syntax: they simply let you do more. That's why you'll see the tilde used often even where .x would serve. As an example, see npm itself: its own package.json file includes lots of dependencies in ~2.4.0 format, rather than the 2.4.x format it could use. By sticking to ~, the syntax is consistent all the way down a list of 70+ versioned dependencies, regardless of which beginning patch number is acceptable.

Anyway, there's still more to SemVer, but I won't try to detail it all here. Check it out on the node semver package's readme. And be sure to use the semantic versioning calculator while you're practicing and trying to get your head around how SemVer works.


RE: Non-Consecutive Version Numbers: OP's final question seems to be about specifying non-consecutive version numbers/ranges (if I have edited it fairly). Yes, you can do that, using the common double-pipe "or" operator: ||. Like so:

"angular": "1.2 <= 1.2.9 || >2.0.0"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What is the difference between Bower and npm?

From Dev

Parsing Bower's Semantic Version Syntax

From Dev

Uninstalling bower installed with a different version of npm

From Dev

How do install a specific version of Bower with NPM

From Dev

What are npm, bower, gulp, Yeoman, and grunt good for?

From Dev

What does ~ mean with regards to semver in bower/npm?

From Dev

Why npm has newer version of packages than bower

From Dev

What is the syntax to checkout a tagged release from SVN with bower?

From Dev

What does Bower mean when it says "unable to find a suitable version"?

From Dev

What is the purpose of syntax support of npm module depcheck?

From Java

NPM/Bower/Composer - differences?

From Dev

What version of npm works with nodejs 0.10?

From Dev

Can't install claymate with npm, error: No compatible version found: bower-config@'>=0.2.0 <0.3.0'

From Dev

After update ember-cli 2.11.0 version how to npm instead of bower?

From Dev

Bower not taking the latest version

From Dev

Specifying version numbers in Bower

From Dev

bower register new version

From Dev

Bower version issue: SockJS

From Dev

bower register new version

From Dev

bower_concat equivalent for NPM

From Dev

Same module/package for Bower and npm

From Dev

Include libraries with Composer, NPM or Bower

From Dev

Include libraries with Composer, NPM or Bower

From Dev

Include a library without npm or bower

From Dev

One command to npm install and bower in npm?

From Dev

How do I find out what version of a bower package is actually installed?

From Dev

What is the correct syntax for launching ipython qtconsole/notebook for python version=x

From Dev

What is the correct syntax for api_version in app.yaml?

From Dev

What is the correct syntax for launching ipython qtconsole/notebook for python version=x

Related Related

  1. 1

    What is the difference between Bower and npm?

  2. 2

    Parsing Bower's Semantic Version Syntax

  3. 3

    Uninstalling bower installed with a different version of npm

  4. 4

    How do install a specific version of Bower with NPM

  5. 5

    What are npm, bower, gulp, Yeoman, and grunt good for?

  6. 6

    What does ~ mean with regards to semver in bower/npm?

  7. 7

    Why npm has newer version of packages than bower

  8. 8

    What is the syntax to checkout a tagged release from SVN with bower?

  9. 9

    What does Bower mean when it says "unable to find a suitable version"?

  10. 10

    What is the purpose of syntax support of npm module depcheck?

  11. 11

    NPM/Bower/Composer - differences?

  12. 12

    What version of npm works with nodejs 0.10?

  13. 13

    Can't install claymate with npm, error: No compatible version found: bower-config@'>=0.2.0 <0.3.0'

  14. 14

    After update ember-cli 2.11.0 version how to npm instead of bower?

  15. 15

    Bower not taking the latest version

  16. 16

    Specifying version numbers in Bower

  17. 17

    bower register new version

  18. 18

    Bower version issue: SockJS

  19. 19

    bower register new version

  20. 20

    bower_concat equivalent for NPM

  21. 21

    Same module/package for Bower and npm

  22. 22

    Include libraries with Composer, NPM or Bower

  23. 23

    Include libraries with Composer, NPM or Bower

  24. 24

    Include a library without npm or bower

  25. 25

    One command to npm install and bower in npm?

  26. 26

    How do I find out what version of a bower package is actually installed?

  27. 27

    What is the correct syntax for launching ipython qtconsole/notebook for python version=x

  28. 28

    What is the correct syntax for api_version in app.yaml?

  29. 29

    What is the correct syntax for launching ipython qtconsole/notebook for python version=x

HotTag

Archive