“ ^”在 package.json 版本控制中是什么意思?

我最近运行了 npm install(npm 1.4.3) ,--save-dev标志和它添加到 package.json 中的包条目都是从 ^开始的,例如 "^2.5.0"。我以前从未在我使用的早期版本的 npm 中看到过这个,而且我找不到任何关于这个符号的文档,只有我已经熟悉的符号,例如 ~>=等等。这是什么意思?

43646 次浏览

Quoting from isaacs/node-semver:

  • ^1.2.3 := >=1.2.3-0 <2.0.0-0 "Compatible with 1.2.3". When using caret operators, anything from the specified version (including prerelease) will be supported up to, but not including, the next major version (or its prereleases). 1.5.1 will satisfy ^1.2.3, while 1.2.2 and 2.0.0-beta will not.
  • ^0.1.3 := >=0.1.3-0 <0.2.0-0 "Compatible with 0.1.3". 0.x.x versions are special: the first non-zero component indicates potentially breaking changes, meaning the caret operator matches any version with the same first non-zero component starting at the specified version.
  • ^0.0.2 := =0.0.2 "Only the version 0.0.2 is considered compatible"

That said, I would recommend to use "~" instead because it has more intuitive semantics, see discussion in npm/npm#4587.

From version ranges

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.

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.