当我使用插入符时,npm 安装了具有不同补丁版本的模块的两个版本

npm installs two versions of a module with different patch versions whilst I'm using caret

我的项目package.json

dependencies: {
    A: "^0.0.2",
    B: "^0.0.1"
}

模块 B package.json

dependencies: {
    A: "^0.0.1",
}

当我 运行 nmp install 为我的项目安装模块 A 两次时。一次在顶层(版本 0.0.2)一次嵌套在模块 B(版本 0.0.1):

project/node_modules/A                   <--0.0.2
project/node_modules/B/node_modules/A    <--0.0.1

但我在两个 package.json 文件中都使用了 ^。那么,为什么即使我重新安装模块 B,它也不使用模块 A 的顶层安装,而是获取模块 A 的嵌套版本?

毕竟,A@^0.0.1 应该匹配 A@0.0.2 对吗?

这是按照设计。看到这个答案

That happens because ^0.0.1 is considered to be equivalent to 0.0.1.

When the version of a package starts with 0 it's considered to be in development, and the semantic versioning rules are different. An increase in either of the numbers can be expected to have breaking changes when the version is 0.0.X. You can see the rules here.

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.

You'll probably see the caret when you increment the minor version, but it will also have the special rules that are mentioned over. "Normal" rules start applying when you increment the major version.

您还可以查看以下页面了解更多详情

https://docs.npmjs.com/misc/semver#caret-ranges-123-025-004

明明是^0.0.3 := >=0.0.3 <0.0.4,意思是^0.0.X等同于0.0.X