What's the difference between tilde(~) and caret(^) in package.json?
What's the difference between tilde(~) and caret(^) in package.json?
问题
After I upgraded to the latest stable node
and npm
, I tried npm install moment --save
. It saves the entry in the package.json
with the caret ^
prefix. Previously, it was a tilde ~
prefix.
- Why are these changes made in
npm
? - What is the difference between tilde
~
and caret^
? - What are the advantages over others?
回答1
See the NPM docs and semver docs:
-
~version
“Approximately equivalent to version”, will update you to all future patch versions, without incrementing the minor version.~1.2.3
will use releases from 1.2.3 to <1.3.0. -
^version
“Compatible with version”, will update you to all future minor/patch versions, without incrementing the major version.^2.3.4
will use releases from 2.3.4 to <3.0.0.
See Comments below for exceptions, in particular for pre-one versions, such as ^0.2.3
回答2
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
value | desc |
---|---|
~version |
"Approximately equivalent to version" See npm semver - Tilde Ranges |
^version |
"Compatible with version" See npm semver - Caret Ranges |
version |
Must match version exactly |
>version |
Must be greater than version |
>=version |
etc |
<version |
|
<=version |
|
1.2.x |
1.2.0, 1.2.1, etc., but not 1.3.0 |
* |
Matches any version |
latest |
Obtains latest release |
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
Official Docs
回答3
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.
The problem is old versions usually don't receive bug fixes that much, so npm uses caret (^
) as the default for --save
.
According to: "Semver explained - why there's a caret (^) in my package.json?".
Note that 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 patch updates, i.e., it behaves the same as the tilde. See "Caret Ranges"
Here's a visual explanation of the concepts:
Source: "Semantic Versioning Cheatsheet".
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2020-12-23 nodemon通过配置文件监视
2020-12-23 nodejs传递参数
2020-12-23 Nodejs cannot find installed module on Windows
2020-12-23 What's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?
2020-12-23 An Absolute Beginner's Guide to Using npm
2020-12-23 前端工具
2020-12-23 Vue cli