JS:NPM依赖包版本号波浪字符"~"

提醒

原帖完整收藏于IT老兵驿站,并会不断更新。

JS:NPM依赖包版本号波浪字符”~”。

正文

官网摘录如下:

Tilde Ranges ~1.2.3 ~1.2 ~1
Allows patch-level changes if a minor version is specified on the comparator. Allows minor-level changes if not.

简单翻译:如果minor被指定,则允许patch被改变;如果没有,允许minor被改变。这里需要理解NPM的版本三元组,NPM采用的是3元组的版本控制,[major,minor,patch]。(详细知识需要参考这里

实例:

~1.2.3 := >=1.2.3 <1.(2+1).0 := >=1.2.3 <1.3.0

minor被指定为2,所以能够获取的版本是大于等于1.2.3,小于1.3之间。

~1.2 := >=1.2.0 <1.(2+1).0 := >=1.2.0 <1.3.0 (Same as 1.2.x)

minor被指定为2,所以能够获取的版本是大于等于1.2,小于1.3之间。

~1 := >=1.0.0 <(1+1).0.0 := >=1.0.0 <2.0.0 (Same as 1.x)

minor没有被指定,所以能够获取的版本是大于等于1,小于2之间,就是minor可以改变。

下面留几道思考题:

~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.

参考

https://github.com/npm/node-semver#functions。