package.json文件中semver说明

  在package.json文件中所依赖的模块版本会有不同的写法,有时候会忘记其用法,现记录如下,以便后续查阅。

   依赖模块都是用一个简单的并且带有包名称和版本范围的对象指定,而这个版本范围是一串由一个或者多个空格分隔符组成的字符串;此外,依赖模块还可以通过tarball或者git URL来标识。注意了,不要将测试工具和转换器模块放置在依赖模块中,这些模块应该放置在开发依赖模块中。

  • version 必须精确匹配版本
  • >version 必须比设置的版本要高
  • >=version 大于或等于该版本
  • <version 小于该版本
  • <=version 小于或等于该版本
  • x-ranges的用法
在[major, minor, patch]的元组中,X、x、*的任何一个符号通常用来替代一个数值,
* := >=0.0.0 任何版本都符号
1.x := >=1.0.0 <2.0.0 匹配主版本(major版本)
1.2.x := >=1.2.0 <1.3.0 匹配主版本和次版本(major and minor versions)
X-range形式的版本,X、x、*这些特殊字符是可选的,如下面的例子所示:
"" (empty string) := * := >=0.0.0
1 := 1.x.x := >=1.0.0 <2.0.0
1.2 := 1.2.x := >=1.2.0 <1.3.0
  • ~version 在比较值中若指定了次要版本(minor version),那么允许patch-level版本可以发生变化;若没有指定次要版本(minor version),那么允许次要版本(minor version)可以发生变化。Tilde Ranges具体用法可以参考下面示例:
~1.2.3 := >=1.2.3 <1.(2+1).0 := >=1.2.3 <1.3.0
~1.2 := >=1.2.0 <1.(2+1).0 := >=1.2.0 <1.3.0 (Same as 1.2.x)
~1 := >=1.0.0 <(1+1).0.0 := >=1.0.0 <2.0.0 (Same as 1.x)
~0.2.3 := >=0.2.3 <0.(2+1).0 := >=0.2.3 <0.3.0
~0.2 := >=0.2.0 <0.(2+1).0 := >=0.2.0 <0.3.0 (Same as 0.2.x)
~0 := >=0.0.0 <(0+1).0.0 := >=0.0.0 <1.0.0 (Same as 0.x)
~1.2.3-beta.2 := >=1.2.3-beta.2 <1.3.0 Note that prereleases in the 1.2.3 version will be allowed, if they are greater than or equal to beta.2. 
So, 1.2.3-beta.4 would be allowed, but 1.2.4-beta.2 would not, because it is a prerelease of a different [major, minor, patch] tuple.
  • ^version 在[major,minor,patch]元组中,当major为非零数字时,允许major和patch发生变化,而major不能发生变化。当major为零数字时,此时只能允许patch发现变化,而major和minor不能发生变化。Caret ranges的具体用法可以参考下面的示例:
^1.2.3 := >=1.2.3 <2.0.0
^0.2.3 := >=0.2.3 <0.3.0
^0.0.3 := >=0.0.3 <0.0.4
^1.2.3-beta.2 := >=1.2.3-beta.2 <2.0.0 Note that prereleases in the 1.2.3 version will be allowed, if they are greater than or equal to beta.2.
So, 1.2.3-beta.4 would be allowed, but 1.2.4-beta.2 would not, because it is a prerelease of a different [major, minor, patch] tuple. ^0.0.3-beta := >=0.0.3-beta <0.0.4 Note that prereleases in the 0.0.3 version only will be allowed, if they are greater than or equal to beta. So, 0.0.3-pr.2 would be allowed. When parsing caret ranges, a missing patch value desugars to the number 0, but will allow flexibility within that value, even if the major and minor versions are both 0. ^1.2.x := >=1.2.0 <2.0.0 ^0.0.x := >=0.0.0 <0.1.0 ^0.0 := >=0.0.0 <0.1.0 A missing minor and patch values will desugar to zero, but also allow flexibility within those values, even if the major version is zero. ^1.x := >=1.0.0 <2.0.0 ^0.x := >=0.0.0 <1.0.0
  • *  匹配任何版本
  • “”  匹配任何版本
  • version1 - version2 类似于 >=version1 <=version2
  • range1 || range2  版本要么满足range1或者range2
  • URL地址放置在一个版本范围的位置处提供指定的模块,在安装时会被下载并且被安装到本地的包中
  • 本地路径作为模块的指定位置,不过在发布到公共仓库时不能使用本地路径
原文地址:https://www.cnblogs.com/bien94/p/12658923.html