Search This Blog

2024/03/20

Node.js:Tilde (~) and carat (^), and their meaning in versioning in package.json


SEMVER stands for Semantic Versioning, which is a versioning scheme used in
software development to assign meaningful versions to software releases.
The Semantic Versioning specification is defined by three numeric components
separated by dots: MAJOR.MINOR.PATCH. Each component has a specific meaning.

In Semantic Versioning (SemVer), if two versions have breaking changes,
the MAJOR version number should be incremented. This is because breaking
changes indicate that there are changes in the software that may cause
existing code to break or behave differently, so incrementing the MAJOR
version number helps communicate this to users and developers clearly.

Npm version are written in 0.0.0 formats, where first number (from left)
stands for major release, second for the minor release and the third for
the latest patch release of this particular version.

Patch updates transcend to bug fixes. In package.json this depiction is
carried forward by ~. Tilde (~) freezes all the previous major and minor
versions. In a way it allows only the latest patched version to be installed
when you are installing this particular dependency package.

For example, ~1.2.0 will update the future patch versions when and so they are released.
We just need to change the 1.2.0 to 1.2.1, 1.2.2, 1.2.3 etc.

Caret (^) notation is used to update automatically minor updates as well as
patch updates. For example, ^1.2.0 will update to 1.3.1 ( if the next minor
patch version is released that way) , thus giving us a 1.X.X fix.

This necessitates us to regularly check whether our code is compatible with
the latest version or not.

Example of package.json

{
"name": "phenix",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "phenix",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"core-js": "^3.36.1"
}
},
"node_modules/core-js": {
"version": "3.36.1",
"resolved": "https://registry.npmjs.org/core-js/-/core-js-3.36.1.tgz",
"integrity": "sha512-BTvUrwxVBezj5SZ3f10ImnX2oRByMxql3EimVqMysepbC9EeMUOpLwdy6Eoili2x6E4kf+ZUB5k/+Jv55alPfA==",
"hasInstallScript": true,
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/core-js"
}
}
}
}


Here "core-js": "^3.36.1" comes with carat sign so when we run

npm i

latest minor version update will be installed
More Information

Value Description
~version Approximately equivalent to version, i.e., only accept new patch versions See npm semver - Tilde Ranges
^version Compatible with version, i.e., accept new minor and patch versions See npm semver - Caret Ranges
version Must match version exactly
>version Must be greater than version
>=version Must be equal or greater than version
<=version Must be equal or lesser than version
1.2.x 1.2.0, 1.2.1, etc., but not 1.3.0
* Matches any version
latest Obtains latest release









No comments:

Post a Comment