JS:NPM依赖包版本号脱字符"^"

个人博客原帖地址

参考官网:https://github.com/npm/node-semver#functions

Caret Ranges ^1.2.3 ^0.2.5 ^0.0.4
Allows changes that do not modify the left-most non-zero digit in the [major, minor, patch] tuple. In other words, this allows patch and minor updates for versions 1.0.0 and above, patch updates for versions 0.X >=0.1.0, and no updates for versions 0.0.X.

Many authors treat a 0.x version as if the x were the major “breaking-change” indicator.

大概意思是:允许的改变不能发生在最左侧非零的数字上,NPM采用的是3元组的版本控制,[major,minor,patch]。换句话说,对于版本1.0.0,允许变更的是minorpatch,对于0.X的,patch可以变更,而对于0.0.X,啥都不能变了。
这里说的改变是说,npm在自动安装时去获取的这个包的版本,如果使用了脱字符或者波浪线等符号,它可以去获取的版本就是在一个范围之内,而不是固定的,这两个符号就是去约束这个范围的。这里还涉及一个版本锁定的概念,涉及yarn的一些理念,回头再讨论。

Caret ranges are ideal when an author may make breaking changes between 0.2.4 and 0.3.0 releases, which is a common practice. However, it presumes that there will not be breaking changes between 0.2.4 and 0.2.5. It allows for changes that are presumed to be additive (but non-breaking), according to commonly observed practices.

“^”这个符号叫做脱字符(caret),这好像是原来打字机的一个功能,现在叫这个名字感觉是有些陌生的。
以下是一些例子,略作一下解释,感觉也没有太多可讲的。

  • ^1.2.3 := >=1.2.3 <2.0.0 (解释:如果是^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

猜你喜欢

转载自blog.csdn.net/chaiyu2002/article/details/81000913