当预发布版本是最新可用版本时,semver 中的插入符 (^) 是否与预发布版本匹配?

Gar*_*iel 7 versioning npm semantic-versioning

考虑使用这些版本的库(例如 NPM 包):

  • 1.0.0
  • 1.0.2
  • 1.1.0-预发布

如果我^1.0.0在依赖项中指定,将安装什么版本?1.1.0-prerelease是最新版本,但我认为任何预发布版本都不满足我未指定预发布部分的范围。我尝试过使用https://semver.npmjs.com,但 lodash 不存在预发布也是最新的情况。

Rob*_*obC 7

通常,带有插入符号 ( ) 前缀的范围(^例如)^1.0.0 不会1.1.0-prerelease导致安装预发行版本。

因此,鉴于您的问题中提供的示例,通常安装的版本是1.0.2.

注意:但是,这种逻辑可能并不总是如此 - 我将很快解释原因。


使用 Semver 计算器的典型逻辑示例:

在撰写本文时,说明使用 Semver 计算器的典型typescript逻辑的更好示例是使用而不是进行测试lodash

使用semver 计算器

  1. 选择typescript作为套餐
  2. 输入一个范围^4.0.0

正如您所看到的,它选择4.0.2和不选择4.2.0-dev.20201204(在 后发布到 npm 注册表4.0.2)。通常会发生这种逻辑。

上述典型逻辑何时可能有所不同:

You'll, have noticed that in my previous explanation I say "typically" alot. I say that because npm has a dist-tag feature that allows the publisher of a package to modify the distribution tags. A short excerpt from the documentation for dist-tag reads as follows:

By default, the latest tag is used by npm to identify the current version of a package, and npm install <pkg> (without any @<version> or @<tag> specifier) installs the latest tag. Typically, projects only use the latest tag for stable release versions, and use other tags for unstable versions such as prereleases.

So, if we consider again the typescript example described in the previous section. If the publisher associated the latest tag in the npm registry with version 4.2.0-dev.20201204. For example if they run the following command:

npm dist-tag add typescript@4.2.0-dev.20201204 latest
Run Code Online (Sandbox Code Playgroud)

then version 4.0.2 will not be installed (given a semver range of ^4.0.0), and instead version 4.2.0-dev.20201204 will be installed.

Similarly, given the examples provided in your question, if we were to associate the latest tag with version 1.1.0-prerelease (using the npm dist-tag ... command), and given a range specified as ^1.0.0 in the dependencies section of your package.json, then version 1.1.0-prerelease will be installed and not 1.0.2.

Note: I would consider these scenarios described in this section as quite rare, (they're certainly not typical but useful to understand), because as stated in that previous excerpt from the docs:

Typically, projects only use the latest tag for stable release versions


Additional info:

Utilize the npm view command to discover information about a package(s) dist tags, particularly the latest tag. For example:

For further info about distribution tags refer to adding dist-tags to packages.