What is the meaning behind those ~ and ^ symbols in your package. Json file?

feature-image

Play all audios:

Loading...

I’m sure you have something like the following in your package.json file:   "dependencies": {     "@angular/animations": "~9.0.1",     "@angular/cdk":


 "^9.0.0"   } Have you ever wondered about the meaning behind those ~ and ^ symbols? Let me explain about SemVer. Versions of the npm packages in the dependencies section of your


package.json file follow what’s called Semantic Versioning (SemVer), an industry standard for software versioning aiming to make it easier to manage dependencies. Libraries, frameworks, or


other tools published on npm should use SemVer in order to clearly communicate what kind of changes projects can expect if they update. This is how Semantic Versioning works according to the


official website: "package": "MAJOR.MINOR.PATCH" * The MAJOR version should increment when you make incompatible API changes (breaking changes). * The MINOR version


should increment when you add functionality in a backwards-compatible manner. * The PATCH version should increment when you make backwards-compatible bug fixes. This means that PATCHes are


bug fixes and MINORs add new features but neither of them break what worked before. Finally, MAJORs add changes that won’t work with earlier versions. Now suppose our project depends on an


npm package called AWESOME-PACKAGE. In our package.json file, we can pin a specific version of this package like this: "awesome-package": "1.2.13" But what if the


maintainer of the package adds bug fixes? You don’t want to miss out on the latest PATCHes because there might be security vulnerabilities with the previous version. That’s when you’ll use


the tilde (~) character. Here’s an example of how to allow updates to any 1.2.x version: "awesome-package": "~1.2.13" But now what if you also want to get MINOR and PATCH


updates? After all, the MINOR and PATCH updates will be backwards-compatible. This means you can update to the latest MINOR and PATCH updates without breaking anything. The caret (^) allows


npm to install future MINOR and PATCH updates. If you want to use version 1.x.x of the awesome-package, you would use: "awesome-package": "^1.2.13" CONCLUSION We use the


tilde (~) and caret (^) characters to get the latest updates without worrying about breaking changes. I hope you’ve found this article useful.