Node.js Semantic Versioning: Everything You Need to Know

It helps in managing dependencies by clearly indicating the nature of changes in new versions. It ensures that developers can confidently update packages without worrying about breaking their applications.

You’ll often see version numbers like 1.2.3 in the package.json file.

Semantic Versioning follows a three-part version number format:

    • MAJOR: This number goes up when you make changes that are not compatible with older versions. It might break things that worked before.

      • MINOR: This number increases when you add new features, but everything that worked before will still work the same way.

      • PATCH: This number is updated when you fix bugs or make small improvements that don’t change how the features work.

          {
            "name": "my-app",
            "version": "1.0.0",
            "dependencies": {
              "express": "^4.17.1",
              "lodash": "~4.17.21"
            }
          }
        

Understanding the Symbols

  • Caret ^: Think of the caret symbol as a roof or a box over the left-most non-zero digit. It protects that digit and ensures it doesn’t change, but anything under that roof can be updated. For example, ^4.17.1 allows updates to any version that starts with 4, like 4.18.0 or 4.20.5, but it won’t let you move to 5.0.0 because that would break through the roof.

  • Tilde ~: Imagine the tilde symbol as a ruler placed right under the patch version. It lets you stretch and update the patch version as much as you want, but it keeps the minor version locked in place. For example, ~4.17.21 will allow updates to any version that starts with 4.17, like 4.17.22 or 4.17.30, but not to 4.18.0 because that would push beyond the ruler’s limit.

  • Practical Examples

  • 1. Major Version Update

  • Let’s say you’re using a package called express with the version 4.17.1. If the developers release a new version 5.0.0, this would be a major update, which means they’ve made some big changes that might break how your app works. If your package.json says "express": "^4.17.1", it won’t automatically update to 5.0.0 because your app might stop working properly.

    You’d need to manually update to version 5.0.0 and probably make some changes to your code to make sure everything still works.

      npm install express@5.0.0
    
  • 2. Minor Version Update

  • Now, imagine the express team releases a version 4.18.0. This is a minor update, meaning they’ve added some new features, but nothing that would break your existing code. Since you’re using "express": "^4.17.1", this update will be automatically picked up when you run npm install, and your app should continue working just fine.

  •     npm update express
    
  • 3. Patch Version Update

  • Finally, let’s say express releases version 4.17.2. This is a patch update, which means they’ve fixed some bugs but haven’t changed any features. Whether you’re using ^4.17.1 or ~4.17.1, your app will automatically get this update when you run npm install, and everything should work as usual, just with fewer bugs.

      npm update express
    

Comment if I have committed any mistake. Let's connect on my socials. I am always open for new opportunities , if I am free :P

Linkedin| Twitter | ShowwCase

0
Subscribe to my newsletter

Read articles from Saurav Maheshwari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Saurav Maheshwari
Saurav Maheshwari