What is tilde (~) and a caret (^) in an npm package.json file?
If you are using npm to manage your app, then you know that we will use package.json for managing dependencies.
sample package.json file |
So as we can see in the above sample package.json file we have some dependencies & dev dependencies as well, now let’s go close to dependencies and try to understand the meaning of tilde & caret symbols.
{
"devDependencies": {
"@angular/common": "5.0.0",
"cordova-plugin-splashscreen": "^5.0.3",
},
"devDependencies": {
"@ionic/app-scripts": "3.2.4",
"@types/chart.js": "^2.7.37",
"typescript": "~2.6.2"
}
}
As we can see above JSON from the package.json file, key is the name of the package and the value is the version of the package to be used.
The version number is in semver syntax which designates each section with a different meaning. It is broken into three sections separated by a dot.
- MAJOR version when you make incompatible API changes,
- MINOR version when you add functionality in a backwards-compatible manner, and
- PATCH version when you make backwards-compatible bug fixes.
"@angular/common": "5.0.0", 5.0.0 major.minor.patch
Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
npm uses the tilde (~) and caret (^) to select which patch and minor versions to use respectively.
So you can see in the above example: “typescript”: “~2.6.2”
it means to install version 2.6.2 or the latest patch version such as 2.6.4.
it means to install version 2.6.2 or the latest patch version such as 2.6.4.
as same with for another example: “cordova-plugin-splashscreen”: “^5.0.3”
it means to install version 5.0.3 or the latest minor or patch version such as 5.1.0.
Understanding tilde(~) and caret(^):
- ~1.2.3 means it should be greater than or equal to 1.2.3 and less than 1.3.0
- ^1.2.3 means it should be greater than or equal to 1.2.3 and less than 2.0.0
If you still have any queries you can comment it out I will try to help you.