语义化npm版本号

参考资料:

在package的devDependencies和dependencies2个字段中有指定依赖包版本,这个版本返回一个由一个或多个空格分隔的字符串,这些版本是有一定的语义的,当然依赖还可以用tarball或者git URL。下面我们来学习下这些版本代表的语义。

语义化版本控制的规范是由Gravatars创办者兼GitHub共同创办者Tom Preston-Werner所建立。

版本号的意义

主版号.次版号.修订号( x.y.z )

  • 主版号:当你做了不相容的 API 修改,
  • 次版号:当你做了向下相容的功能性新增,
  • 修订号:当你做了向下相容的问题修正。

指定版本

0.1.2 //指定依赖版本为0.1.2

范围依赖

在指定范围内选择稳定版本进行安装

< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to

console.info(semver.satisfies('1.2.3', '<1.2.0')); //false
console.info(semver.satisfies('1.2.3', '<1.3.0')); //true
console.info(semver.satisfies('1.2.3', '<1.2')); //false
console.info(semver.satisfies('1.2.3', '<1.3')); //true

console.info(semver.satisfies('1.2.3', '>1.2')); //false
console.info(semver.satisfies('1.2.3', '>1.2.0')); //true

console.info(semver.satisfies('1.2.3', '>1.2 || <1.2')); //true
console.info(semver.satisfies('1.2.0', '>1.2 || <1.2')); //true

高级范围选择

1.2.3 - 2.3.4  //>=1.2.3 <=2.3.4
1.2 - 2.3.4 // >=1.2.0 <=2.3.4
1.2.3 - 2.3 //>=1.2.3 <2.4.0
1.2.3 - 2 // >=1.2.3 <3.0.0
* // >=0.0.0
1.x // >=1.0.0 <2.0.0
1.2.x // >=1.2.0 <1.3.0
"" (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

波浪范围选择

介于当前版本和副版本+1之间的版本,如不存在副版本,则介于当前版本和主版本+1版本

~1.2.3 // >=1.2.3 <1.3.0
~1.2 // >=1.2.0 <1.3.0 (Same as 1.2.x)
~1 // >=1.0.0 <2.0.0 (Same as 1.x)
~0.2.3 // >=0.2.3 <0.3.0
~0.2 // = >=0.2.0 <0.3.0 (Same as 0.2.x)
~0 // >=0.0.0 <1.0.0 (Same as 0.x)

插入号范围选择

在指定了副版本情况下或主版本不为0情况下,介于当前版本和主版本+1之间的版本,其他情况介于当前版本和副版本+1之间

^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.x // >=1.2.0 <2.0.0
^0.0.x // >=0.0.0 <0.1.0
^0.0 // >=0.0.0 <0.1.0
^1.x // >=1.0.0 <2.0.0
^0.x // >=0.0.0 <1.0.0
posted @ 2017-03-11 11:27  buzzjan  阅读(481)  评论(0编辑  收藏  举报