Npm install 忽略版本号中的波浪号 (~) [英] Npm install ignores tilde (~) in version number
问题描述
我想安装 1.8.x 版本的软件包,并且能够稍后在 >=1.8.0 <1.9.0 范围内自动更新此依赖项.
I would like to install 1.8.x version a package, and be able to later automatically update this dependency inside the >=1.8.0 <1.9.0 range.
我尝试运行此命令:
npm install example-package@~1.8 --save
不幸的是,它将此记录添加到我的 package.json
中:
Unfortunately it adds this record to my package.json
:
"example-package" : "^1.8.0"
但我想要的是:
"example-package" : "~1.8.0"
如何使用 npm install
做到这一点,而无需手动编辑 package.json
文件?
How is it possible to do it with npm install
, without manually edit the package.json
file?
推荐答案
semver 前缀由 save-prefix
配置.默认值是一个插入符号 (^
),您可以通过运行以下命令进行检查 npm config
命令:
The semver prefix is defined by the save-prefix
config. The default value is a caret (^
) which you can check by running the following npm config
command:
npm config get save-prefix
不幸的是,npm install
命令没有指定选项这个,所以你需要做的是:
Unfortunately, the npm install
command has no option to specify this, so what you'll need to do is:
通过运行将
save-prefix
值设置为波浪号 (~
):
Set the
save-prefix
value to a tilde (~
) by running:
npm config set save-prefix="~"
运行以下命令安装您的软件包:
Install your package by running:
npm i example-package@1.8.0 --save
注意: 波浪号 (~
) 不得包含在安装命令中.
Note: The tilde (~
) must not be included in the install command.
最后,通过运行将 save-prefix
值设置回它的默认值,即插入符号 (^
):
Finally, set the save-prefix
value back to it's default, i.e. a caret (^
) by running:
npm config delete save-prefix
注意:如果您希望以后的所有 npm install
都使用波浪号 (~
),您就不会执行最后一步前缀而不是插入符号 (^
).
Note: You wouldn't do this last step if you wanted all future npm install
's to use the tilde (~
) prefix instead of a caret (^
).
以上步骤会在package.json
中添加如下记录:
The above steps will add the following record in package.json
:
"example-package" : "~1.8.0"
注意波浪号 ~
而不是默认的插入符号 ^
Note the tilde ~
instead of the default caret ^
您可以使用 &&
运算符将上述命令组合成一个复合命令.例如:
You can utilize the &&
operator to combine the aforementioned commands into a compound command. For instance:
npm config set save-prefix="~" && npm i example-package@1.8.0 --save && npm config delete save-prefix
这篇关于Npm install 忽略版本号中的波浪号 (~)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!