How npm mange package versions: Tilde(~) vs Caret(^)

How npm mange package versions

All the developers who worked with node or javascript might see some special charaters before the version of the packages we installed. And thes are quite common for all other frameworks as well.

This is how npm manage package versions. Using these special characters, NPM decides which version of the package to install in order to run the project properly. Knowing these will help us handle version mismatches.

All our angular application builds failed, and this is what we did.


The plain number:

package_sample : "1.2.3"

Adding a package version without any special characters will make npm to install the exact specified version of the package.

Tilde (~):

package_sample : "~1.2.3"

Tilde(~) allows npm to install upto new minor version of the package. Npm may install a future version 1.2.4 or 1.3.5. This helps in resolving the minor issues on the go.

Caret (^):

package_sample : "^1.2.3"

Caret(^) allows npm to install upto next major version of the package. Npm can install a future version 1.2.5 or 1.3.0 or 2.0.0. And this is the most prefered way.

Or (||):

package_sample : "1.2.3 || 1.4.0"

Or(||) allows npm to install either 1.2.3 or 1.4.0 versions of the package. Usually helps when we’ve cached date of a specific version of the package.

Dash (-):

package_sample : "1.2.3-1.2.7"

Dash(-) indicates a range of versions of the package. Npm can use any version in the range 1.2.3 to 1.2.7

>,>=,<,<=:

These operators will as they mathematically mean.

package_sample : ">1.2.3"     // An version greater than 1.2.3
package_sample : ">=1.2.3"   // An version greater than or equals to 1.2.3
package_sample : "<1.2.3"   // An version less than 1.2.3
package_sample : "<=1.2.3" // An version less than or equals to 1.2.3

Start (*):

package_sample : "*"

Star(*) allows npm to go with the most latest version of he package despite of the compatibility. This is no at all recommended and may broke your code at some point of time.

Combination:

package_sample : ">=1.2.3 <2.0.0"
package_sample : "<2.0.0 || >2.0.0"

You may combine any of these characters to create a new custom range. It allows us to have more conroll the versions.


Conclusion

This is all you need to know about how npm mange package versions and most of the frameworks follow the same notation. Feel free to comment, where else you see the same notation.

Share this article
Shareable URL
Leave a Reply

Your email address will not be published. Required fields are marked *

Read next
Subscribe to our newsletter