node.js package.json 中的波浪号(~) 和插入符号(^) 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22343224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between tilde(~) and caret(^) in package.json?
提问by Fizer Khan
After I upgraded to latest stable nodeand npm, I tried npm install moment --save. It saves the entry in the package.jsonwith the caret ^prefix. Previously, it was a tilde ~prefix.
我已经升级到最新的稳定后node和npm,我试过npm install moment --save。它package.json使用插入符号^前缀保存条目。以前,它是波浪号~前缀。
- Why are these changes made in
npm? - What is the difference between tilde
~and caret^? - What is the advantages over others?
- 为什么要进行这些更改
npm? - 波浪号
~和插入符号有^什么区别? - 和别人相比有什么优势?
回答by jgillich
See the NPM docsand semver docs
~version “Approximately equivalent to version”, will update you to all future patch versions, without incrementing the minor version. ~1.2.3will use releases from 1.2.3 to <1.3.0.
~version “大约相当于版本”,会将您更新到所有未来的补丁版本,而不会增加次要版本。~1.2.3将使用从 1.2.3 到 <1.3.0 的版本。
^version “Compatible with version”, will update you to all future minor/patch versions, without incrementing the major version. ^2.3.4will use releases from 2.3.4 to <3.0.0.
^version “与版本兼容”,会将您更新到所有未来的次要/补丁版本,而不会增加主要版本。^2.3.4将使用从 2.3.4 到 <3.0.0 的版本。
See Comments below.
请参阅下面的评论。
回答by Ahmad
I would like to add the official npmjs documentation as well which describes all methods for version specificity including the ones referred to in the question -
我还想添加官方 npmjs 文档,其中描述了版本特异性的所有方法,包括问题中提到的方法 -
https://docs.npmjs.com/files/package.json
https://docs.npmjs.com/files/package.json
https://docs.npmjs.com/misc/semver#x-ranges-12x-1x-12-
https://docs.npmjs.com/misc/semver#x-ranges-12x-1x-12-
~version"Approximately equivalent to version" See npm semver - Tilde Ranges& semver (7)^version"Compatible with version" See npm semver - Caret Ranges& semver (7)versionMust match version exactly>versionMust be greater than version>=versionetc<version<=version1.2.x1.2.0, 1.2.1, etc., but not 1.3.0http://sometarballurl(this may be the URL of a tarball which will be downloaded and installed locally*Matches any versionlatestObtains latest release
~version“大约相当于版本”参见npm semver - Tilde Ranges& semver (7)^version“与版本兼容”参见npm semver - Caret Ranges& semver (7)version必须完全匹配版本>version必须大于版本>=version等等<version<=version1.2.x1.2.0、1.2.1 等,但不是 1.3.0http://sometarballurl(这可能是将在本地下载并安装的 tarball 的 URL*匹配任何版本latest获取最新版本
The above list is not exhaustive. Other version specifiers include GitHub urls and GitHub user repo's, local paths and packages with specific npm tags
上面的列表并不详尽。其他版本说明符包括 GitHub url 和 GitHub 用户存储库、本地路径和带有特定 npm 标签的包
回答by pspi
npm allows installing newer version of a package than the one specified. Using tilde (~) gives you bug fix releases and caret (^) gives you backwards-compatible new functionality as well.
npm 允许安装比指定版本更新的包。使用波浪号 ( ~) 为您提供错误修复版本,而插入符号 ( ^) 也为您提供向后兼容的新功能。
The problem is old versions usually don't receive bug fixes that much, so npm uses caret (^) as the default for --save.
问题是旧版本通常不会收到那么多的错误修复,因此 npm 使用插入符号 ( ^) 作为--save.
According to: "Semver explained - why there's a caret (^) in my package.json?".
根据:“Semver 解释了 - 为什么我的 package.json 中有插入符号 (^)?” .
Notethat the rules apply to versions above 1.0.0 and not every project follows semantic versioning. For versions 0.x.x the caret allows only patchupdates, i.e., it behaves the same as the tilde. See "Caret Ranges"
请注意,规则适用于 1.0.0 以上的版本,并非每个项目都遵循语义版本控制。对于 0.xx 版本,插入符号只允许补丁更新,即它的行为与波浪号相同。请参阅“插入符范围”
Here's a visual explanation of the concepts:
以下是这些概念的直观解释:
Source: "Semantic Versioning Cheatsheet".
来源:“语义版本控制备忘单”。
回答by rofrol
Semver
森弗
<major>.<minor>.<patch>-beta.<beta> == 1.2.3-beta.2
- Use npm semver calculatorfor testing. (Although the explanations for ^ (include everything greater than a particular version in the same major range) and ~ (include everything greater than a particular version in the same minor range) aren't a 100% correct, the calculator seems to work fine)
- Alternatively, use SemVer Checkinstead, which doesn't require you to pick a package and also offers explanations.
- 使用npm semver 计算器进行测试。(尽管对 ^(包括大于同一主要范围内特定版本的所有内容)和 ~(包括大于同一次要范围内特定版本的所有内容)的解释不是 100% 正确,但计算器似乎工作正常)
- 或者,使用SemVer Check代替,它不需要您选择一个包并且还提供说明。
Allow or disallow changes
允许或禁止更改
- Pin version:
1.2.3. - Use
^(like head). Allows updates at the second non-zero level from the left:^0.2.3means0.2.3 <= v < 0.3. - Use
~(like tail). Generally freeze right-most level or set zero if omitted:~1means1.0.0 <= v < 2.0.0~1.2means1.2.0 <= v < 1.3.0.~1.2.4means1.2.4 <= v < 1.3.0.
- Ommit right-most level:
0.2means0.2 <= v < 1. Differs from~because:- Starting omitted level version is always
0 - You can set starting major version without specifying sublevels.
- Starting omitted level version is always
- 引脚版本:
1.2.3。 - 使用
^(如头部)。允许在左起第二个非零级别进行更新:^0.2.3means0.2.3 <= v < 0.3。 - 使用
~(如尾巴)。通常冻结最右边的级别或如果省略则设置为零:~1方法1.0.0 <= v < 2.0.0~1.2是指1.2.0 <= v < 1.3.0。~1.2.4是指1.2.4 <= v < 1.3.0。
- 忽略最右边的级别:
0.2意味着0.2 <= v < 1。不同于~因为:- 开始省略级别版本总是
0 - 您可以在不指定子级别的情况下设置起始主要版本。
- 开始省略级别版本总是
All (hopefully) possibilities
所有(希望)可能性
Set starting major-level and allow updates upward
设置起始主要级别并允许向上更新
* or "(empty string) any version
1 v >= 1
Freeze major-level
冻结专业级
~0 (0) 0.0 <= v < 1
0.2 0.2 <= v < 1 // Can't do that with ^ or ~
~1 (1, ^1) 1 <= v < 2
^1.2 1.2 <= v < 2
^1.2.3 1.2.3 <= v < 2
^1.2.3-beta.4 1.2.3-beta.4 <= v < 2
Freeze minor-level
冻结次要级别
^0.0 (0.0) 0 <= v < 0.1
~0.2 0.2 <= v < 0.3
~1.2 1.2 <= v < 1.3
~0.2.3 (^0.2.3) 0.2.3 <= v < 0.3
~1.2.3 1.2.3 <= v < 1.3
Freeze patch-level
冻结补丁级别
~1.2.3-beta.4 1.2.3-beta.4 <= v < 1.2.4 (only beta or pr allowed)
^0.0.3-beta 0.0.3-beta.0 <= v < 0.0.4 or 0.0.3-pr.0 <= v < 0.0.4 (only beta or pr allowed)
^0.0.3-beta.4 0.0.3-beta.4 <= v < 0.0.4 or 0.0.3-pr.4 <= v < 0.0.4 (only beta or pr allowed)
Disallow updates
禁止更新
1.2.3 1.2.3
^0.0.3 (0.0.3) 0.0.3
Notice: Missing major, minor, patch or specifying betawithout number, is the same as anyfor the missing level.
注意:缺少major、minor、patch 或未指定beta编号,any与缺少级别相同。
Notice: When you install a package which has 0as major level, the update will only install new beta/pr level version! That's because npmsets ^as default in package.jsonand when installed version is like 0.1.3, it freezes all major/minor/patch levels.
注意:当您安装一个0主要级别的软件包时,更新只会安装新的 beta/pr 级别版本!那是因为在安装版本中npm设置^为默认值,package.json并且在安装版本时0.1.3,它会冻结所有主要/次要/补丁级别。
回答by alex
~fixes major and minor numbers. It is used when you're ready to accept bug-fixes in your dependency, but don't want any potentially incompatible changes.
~修复主要和次要号码。当您准备好接受依赖项中的错误修复,但不想要任何潜在的不兼容更改时,将使用它。
^fixes the major number only. It is used when you're closely watching your dependencies and are ready to quickly change your code if minor release will be incompatible.
^仅修复主要编号。当您密切关注您的依赖项并准备在次要版本不兼容时快速更改您的代码时使用它。
In addition to that, ^is not supportedby old npm versions, and should be used with caution.
除此之外,^是不支持由旧版本的NPM,并应谨慎使用。
So, ^is a good default, but it's not perfect. I suggest to carefully pick and configure the semver operator that is most useful to you.
所以,^是一个很好的默认值,但它并不完美。我建议仔细选择和配置对您最有用的 semver 运算符。
回答by haotang
~: Reasonably closeto
~:合理接近至
~1.1.5: 1.1.0 <= accepted < 1.2.0
^: Compatiblewith
^:兼容与
^1.1.5: 1.1.5 <= accepted < 2.0.0
^0.1.3: 0.1.3 <= accepted < 0.2.0
^0.0.4: 0.0.4 <= accepted < 0.1.0
回答by Will Stern
^is 1.[any].[any] (latest minor version)~is 1.2.[any] (latest patch)
^是 1.[any].[any](最新的次要版本)~是 1.2.[any](最新的补丁)
A great read is this blog poston how semver applies to npm
and what they're doing to make it match the semver standard
http://blog.npmjs.org/post/98131109725/npm-2-0-0
一个很好的阅读是这篇关于 semver 如何适用于 npm
以及他们正在做什么以使其符合semver 标准的博客文章
http://blog.npmjs.org/post/98131109725/npm-2-0-0
回答by catamphetamine
Hat matching may be considered "broken" because it wont update ^0.1.2to 0.2.0. When the software is emerging use 0.x.yversions and hat matching will only match the last varying digit (y). This is done on purpose. The reason is that while the software is evolving the API changes rapidly: one day you have these methods and the other day you have those methods and the old ones are gone. If you don't want to break the code for people who already are using your library you go and increment the major version: e.g. 1.0.0-> 2.0.0-> 3.0.0. So, by the time your software is finally 100% done and full-featured it will be like version 11.0.0and that doesn't look very meaningful, and actually looks confusing. If you were, on the other hand, using 0.1.x-> 0.2.x-> 0.3.xversions then by the time the software is finally 100% done and full-featured it is released as version 1.0.0and it means "This release is a long-term service one, you can proceed and use this version of the library in your production code, and the author won't change everything tomorrow, or next month, and he won't abandon the package".
帽子匹配可能被视为“损坏”,因为它不会更新^0.1.2到0.2.0. 当软件出现时,使用0.x.y版本和帽子匹配只会匹配最后一个不同的数字 ( y)。这是故意这样做的。原因是,虽然软件在不断发展,但 API 变化很快:有一天您拥有这些方法,而有一天您拥有这些方法,而旧的方法则消失了。如果您不想为已经在使用您的库的人破坏代码,您可以增加主要版本:例如1.0.0-> 2.0.0-> 3.0.0。因此,当您的软件最终 100% 完成并具有全功能时,它将像版本一样11.0.0,看起来没有什么意义,实际上看起来很混乱。另一方面,如果您使用0.1.x->0.2.x->0.3.x版本然后当软件最终 100% 完成并且功能齐全时,它会作为版本发布1.0.0,这意味着“此版本是一个长期服务版本,您可以继续并在您的生产中使用此版本的库代码,而且作者明天或下个月不会改变一切,他不会放弃这个包”。
The rule is: use 0.x.yversioning when your software hasn't yet matured and release it with incrementing the middle digit when your public API changes (therefore people having ^0.1.0won't get 0.2.0update and it won't break their code). Then, when the software matures, release it under 1.0.0and increment the leftmost digit each time your public API changes (therefore people having ^1.0.0won't get 2.0.0update and it won't break their code).
规则是:0.x.y当您的软件尚未成熟时使用版本控制,并在您的公共 API 更改时增加中间数字来发布它(因此人们^0.1.0不会获得0.2.0更新,也不会破坏他们的代码)。然后,当软件成熟时,在1.0.0每次公共 API 更改时将其发布并增加最左边的数字(因此人们^1.0.0不会得到2.0.0更新,也不会破坏他们的代码)。
Given a version number MAJOR.MINOR.PATCH, increment the:
MAJOR version when you make incompatible API changes,
MINOR version when you add functionality in a backwards-compatible manner, and
PATCH version when you make backwards-compatible bug fixes.
回答by Laxmi
~ Tilde:
~ 波浪号:
~freezes major and minor numbers.- It is used when you're ready to accept bug-fixes in your dependency, but don't want any potentially incompatible changes.
- The tilde matches the most recent minor version(the middle number).
- ~1.2.3 will match all 1.2.x versions, but it will miss 1.3.0.
- Tilde (~) gives you bug fix releases
~冻结主要和次要号码。- 当您准备好接受依赖项中的错误修复,但不想要任何潜在的不兼容更改时,将使用它。
- 波浪号与最新的次要版本(中间数字)相匹配。
- ~1.2.3 将匹配所有 1.2.x 版本,但会错过 1.3.0。
- 波浪号 (~) 为您提供错误修复版本
^ Caret:
^ 插入符号:
^freezes the major number only.- It is used when you're closely watching your dependencies and are ready to quickly change your code if minor release will be incompatible.
- It will update you to the most recent major version(the first number).
- ^1.2.3 will match any 1.x.x release including 1.3.0, but it will hold off on 2.0.0.
- Caret (^) gives you backwards-compatible new functionality as well.
^只冻结主号码。- 当您密切关注您的依赖项并准备在次要版本不兼容时快速更改您的代码时使用它。
- 它会将您更新到最新的主要版本(第一个数字)。
- ^1.2.3 将匹配包括 1.3.0 在内的任何 1.xx 版本,但它会延迟 2.0.0。
- 插入符号 (^) 也为您提供向后兼容的新功能。
回答by Mudassir
Tilde ~matches minor version, if you have installed a package that has 1.4.2 and after your installation, versions 1.4.3 and 1.4.4 are also available if in your package.json it is used as ~1.4.2 then npm install in your project after upgrade will install 1.4.4 in your project. But there is 1.5.0 available for that package then it will not be installed by ~. It is called minor version.
波浪号 ~匹配次要版本,如果你安装了一个有 1.4.2 的包,并且在你安装之后,1.4.3 和 1.4.4 版本也可用,如果在你的 package.json 中它被用作 ~1.4.2 然后 npm install在您的项目升级后将在您的项目中安装 1.4.4。但是该软件包有 1.5.0 可用,那么它不会被 ~ 安装。它被称为次要版本。
Caret ^matches major version, if 1.4.2 package is installed in your project and after your installation 1.5.0 is released then ^ will install major version. It will not allow to install 2.1.0 if you have ^1.4.2.
插入符号 ^匹配主要版本,如果您的项目中安装了 1.4.2 包,并且在您的安装 1.5.0 发布后,则 ^ 将安装主要版本。如果您有^1.4.2,则不允许安装 2.1.0 。
Fixed versionif you don't want to change version of package on each installation then used fixed version with out any special character e.g "1.4.2"
固定版本,如果您不想在每次安装时更改包的版本,则使用不带任何特殊字符的固定版本,例如“1.4.2”
Latest Version *If you want to install latest version then only use * in front of package name.
最新版本 *如果你想安装最新版本,那么只在包名前使用 *。


