node.js Bower(和 npm)版本语法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19030170/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:45:20  来源:igfitidea点击:

What is the bower (and npm) version syntax?

node.jsbowersemantic-versioning

提问by Samuel Hapak

Bower enables me to specify version requirements for packages using the following syntax:

Bower 使我能够使用以下语法指定包的版本要求:

"dependencies": {
  "<name>": "<version>",
},

But I have not been able to find what is the syntax to use for the <version>. I know that I can specify versions to be:

但我一直无法找到用于<version>. 我知道我可以指定版本为:

  • greater than a certain version with ">1.0.0"
  • greater than or equal to a version: ">=1.0.0"
  • or in some range: "1.0.0 - 2.0.0".
  • 大于某个版本 ">1.0.0"
  • 大于或等于一个版本: ">=1.0.0"
  • 或在某个范围内:"1.0.0 - 2.0.0".

I also know that there is a common version syntax containing the tilde: "~1.0.0". But I am not sure what it means and whether it is the same as "=1.0.0".

我也知道有一个包含波浪号的通用版本语法:"~1.0.0". 但我不确定它是什么意思以及它是否与"=1.0.0".

I am also interested to know whether I am able to specify multiple non-consecutive versions, such as exactly 1.0.3plus versions greater than 1.5.0, etc...

我也有兴趣知道我是否能够指定多个非连续版本,例如完全1.0.3加上大于 的版本1.5.0等......

回答by XML

In a nutshell, the syntax for Bower version numbers (and NPM's) is called SemVer, which is short for 'Semantic Versioning'. You can find documentation for the detailed syntax of SemVer as used in Bower and NPM on the API for the semver parser within Node/npm. You can learn more about the underlying spec (which does notmention ~or other syntax details) at semver.org.

简而言之,Bower 版本号(和 NPM 的)的语法称为 SemVer,它是“语义版本控制”的缩写。您可以在 Node/npm 中的 semver 解析器的 API 上找到有关 Bower 和 NPM 中使用的 SemVer 的详细语法的文档。您可以在semver.org 上了解有关底层规范(提及~或其他语法细节)的更多信息

There's a super-handy visual semver calculatoryou can play with, making all of this much easier to grok and test.

您可以使用一个超级方便的视觉 semver 计算器,使所有这些都更容易理解和测试。

SemVer isn't just a syntax! It has some pretty interesting things to say about the right ways to publish API's, which will help to understand what the syntax means. Crucially:

SemVer 不仅仅是一种语法!关于发布 API 的正确方法,它有一些非常有趣的事情要说,这将有助于理解语法的含义。至关重要的是:

Once you identify your public API, you communicate changes to it with specific increments to your version number. Consider a version format of X.Y.Z (Major.Minor.Patch). Bug fixes not affecting the API increment the patch version, backwards compatible API additions/changes increment the minor version, and backwards incompatible API changes increment the major version.

一旦您确定了您的公共 API,您就可以通过版本号的特定增量来传达对它的更改。考虑 XYZ (Major.Minor.Patch) 的版本格式。不影响 API 的错误修复会增加补丁版本,向后兼容的 API 添加/更改会增加次要版本,向后不兼容的 API 更改会增加主要版本。

So, your specific question about ~relates to that Major.Minor.Patch schema. (As does the related caret operator ^.) You can use ~to narrow the range of versions you're willing to accept to either:

因此,您的具体问题~与 Major.Minor.Patch 架构有关。(与相关的插入符操作符一样^。)您可以使用~将您愿意接受的版本范围缩小到:

  • subsequent patch-levelchanges to the same minor version ("bug fixes not affecting the API"), or:
  • subsequent minor-levelchanges to the same major version ("backwards compatible API additions/changes")
  • 对同一次要版本的后续补丁级别更改(“不影响 API 的错误修复”),或:
  • 对同一主要版本的后续次要更改(“向后兼容的 API 添加/更改”

For example: to indicate you'll take any subsequent patch-level changes on the 1.2.x tree, starting with 1.2.0, but less than 1.3.0, you could use:

例如:要表明您将在 1.2.x 树上进行任何后续补丁级别更改,从 1.2.0 开始,但小于 1.3.0,您可以使用:

"angular": "~1.2"
  or:
"angular": "~1.2.0"

This also gets you the same results as using the .xsyntax:

这也为您提供与使用.x语法相同的结果:

"angular": "1.2.x"

But, you can use the tilde/~syntax to be even more specific: if you're only willing to accept patch-level changes starting with 1.2.4, but still less than 1.3.0, you'd use:

但是,您可以使用波浪号/~语法更具体:如果您只愿意接受从 1.2.4 开始但仍然小于 1.3.0 的补丁级别更改,则可以使用:

"angular": "~1.2.4"

Moving left, towards the majorversion, if you use...

向左移动,朝向主要版本,如果您使用...

"angular": "~1"

... it's the same as...

……和……一样

"angular": "1.x"
  or:
"angular": "^1.0.0"

...and matches any minor- or patch-level changes above 1.0.0, and less than 2.0:

...并匹配任何高于 1.0.0 且低于 2.0 的次要或补丁级别更改:

Note that last variation above: it's called a 'caret range'. The caret looks an awful lot like a >, so you'd be excused for thinking it means "any version greater than1.0.0". (I've certainly slipped on that.) Nope!

请注意上面的最后一个变体:它被称为'caret range'。插入符号看起来非常像>,因此您可以认为它的意思是“任何大于1.0.0 的版本”。(我当然滑倒了。)不!

Caret ranges are basically used to say that you care onlyabout the left-most significant digit - usually the major version - and that you'll permit any minor- or patch-level changes that don't affect that left-most digit. Yet, unlike a tilde range that specifies a major version, caret ranges let you specify a precise minor/patch starting point. So, while ^1.0.0 === ~1, a caret range such as ^1.2.3lets you say you'll take any changes >=1.2.3 && <2.0.0. You couldn't do that with a tilde range.

插入符号范围基本上用于表示您关心最左边的有效数字 - 通常是主要版本 - 并且您将允许任何不影响最左边数字的次要或补丁级别更改。然而,与指定主要版本的波浪号范围不同,插入符范围允许您指定精确的次要/补丁起始点。因此, while ^1.0.0 === ~1,插入符号范围,例如^1.2.3让您说您将进行任何更改>=1.2.3 && <2.0.0。你不能用波浪号范围做到这一点。

That all seems confusing at first, when you look at it up-close. But zoom out for a sec, and think about it this way: the caret simply lets you say that you're most concerned about whatever significant digit is left-most. The tilde lets you say you're most concerned about whichever digit is right-most.The rest is detail.

当您近距离观察时,这一切乍一看似乎令人困惑。但是缩小一秒钟,然后这样想:插入符号只是让您说您最关心最左边的任何有效数字。波浪号让你说你最关心最右边的那个数字。剩下的就是细节。

It's the expressive power of the tilde and the caret that explains why people use them much more than the simpler .xsyntax: they simply let you do more. That's why you'll see the tilde used often even where .xwould serve. As an example, see npm itself: its own package.json file includes lots of dependencies in ~2.4.0format, rather than the 2.4.xformat it coulduse. By sticking to ~, the syntax is consistent all the way down a list of 70+ versioned dependencies, regardless of which beginning patch number is acceptable.

波浪号和插入符号的表达能力解释了为什么人们使用它们比简单的.x语法要多得多:它们只是让你做更多的事情。这就是为什么您会看到经常使用波浪号的原因,即使在适用的地方.x也是如此。例如,请参见 npm 本身:它自己的 package.json 文件在~2.4.0格式上包含许多依赖项,而不是2.4.x可以使用的格式。通过坚持~,语法在 70 多个版本依赖项列表中一直保持一致,无论哪个起始补丁号是可接受的。

Anyway, there's still more to SemVer, but I won't try to detail it all here. Check it out on the node semver package's readme. And be sure to use the semantic versioning calculatorwhile you're practicing and trying to get your head around how SemVer works.

无论如何,SemVer 还有更多内容,但我不会在这里详细介绍。在node semver 包的 readme上查看。并且一定要使用语义版本的计算器,当你练习,并试图围绕让你的头SemVer是如何工作的。



RE: Non-Consecutive Version Numbers: OP's final question seems to be about specifying non-consecutive version numbers/ranges (if I have edited it fairly). Yes, you can do that, using the common double-pipe "or" operator: ||. Like so:

RE:非连续版本号:OP 的最后一个问题似乎是关于指定非连续版本号/范围(如果我已经对其进行了合理编辑)。是的,你可以做到这一点,使用常见的双管“或”运算符:||。像这样:

"angular": "1.2 <= 1.2.9 || >2.0.0"

回答by Jerome Anthony

Based on semver, you can use

基于semver,您可以使用

  • Hyphen Ranges X.Y.Z - A.B.C1.2.3-2.3.4 Indicates >=1.2.3 <=2.3.4

  • X-Ranges1.2.x 1.X 1.2.*

  • Tilde Ranges~1.2.3 ~1.2 Indicates allowing patch-level changes or minor version changes.

  • 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

    • ^1.2.x(means >=1.2.0 <2.0.0)
    • ^0.0.x(means >=0.0.0 <0.1.0)
    • ^0.0(means >=0.0.0 <0.1.0)
  • 连字符范围 XYZ - ABC1.2.3-2.3.4 表示>=1.2.3 <=2.3.4

  • X 范围1.2.x 1.X 1.2.*

  • 波浪号范围~1.2.3 ~1.2 表示允许补丁级别更改或次要版本更改。

  • 插入符号范围 ^1.2.3 ^0.2.5 ^0.0.4

    允许不修改 [major, minor, patch] 元组中最左边的非零数字的更改

    • ^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)

回答by Wilfred Hughes

Bower uses semver syntax, but here are a few quick examples:

Bower 使用semver 语法,但这里有几个简单的例子:

You can install a specific version:

您可以安装特定版本:

$ bower install jquery#1.11.1

You can use ~ to specify 'any version that starts with this':

您可以使用 ~ 来指定“以此开头的任何版本”:

$ bower install jquery#~1.11

You can specify multiple version requirements together:

您可以一起指定多个版本要求:

$ bower install "jquery#<2.0 >1.10"

回答by shacker

You can also use the latestkeyword to install the most recent version available:

您还可以使用latest关键字来安装可用的最新版本:

  "dependencies": {
    "fontawesome": "latest"
  }

回答by Decima

If there is no patch number, ~is equivalent to appending .xto the non-tilde version. If there is a patch number, ~allows all patch numbers >= the specified one.

如果没有补丁号,~就相当于追加.x到非波浪号版本。如果有补丁编号,则~允许所有补丁编号 >= 指定的编号。

~1     := 1.x
~1.2   := 1.2.x
~1.2.3 := (>=1.2.3 <1.3.0)

I don't have enough points to comment on the accepted answer, but some of the tilde information is at odds with the linked semver documentation: "angular": "~1.2"will notmatch 1.3, 1.4, 1.4.9. Also "angular": "~1"and "angular": "~1.0"are notequivalent. This can be verified with the npm semver calculator.

我没有足够的分数来评论已接受的答案,但一些波浪号信息与链接的 semver 文档不一致:"angular": "~1.2"匹配1.3、1.4、1.4.9 。也"angular": "~1""angular": "~1.0"等价的。这可以通过npm semver 计算器进行验证。