avatarBhargav Bachina

Summary

This article provides a step-by-step guide on how to update project versioning with the npm tool for NodeJS projects, using SemVer as the versioning standard.

Abstract

The article explains the importance of versioning in the DevOps world, where applications are deployed frequently to meet customer needs. It introduces SemVer, a versioning standard that consists of three numbers separated by a dot: major, minor, and patch. The article then provides a sample NodeJS project and explains how to create a release, update the patch version, update the minor version, and update the major version using the npm tool. The article also emphasizes the importance of tagging releases for easier rollback and debugging.

Opinions

  • The author believes that versioning is important in the DevOps world to meet customer needs and to enable easy rollback and debugging.
  • The author recommends using SemVer as the versioning standard for NodeJS projects.
  • The author provides a step-by-step guide on how to update project versioning with the npm tool, which can be helpful for developers working on NodeJS projects.
  • The author emphasizes the importance of tagging releases for easier rollback and debugging.

How To Update Project Versioning With npm tool

A Step by Step guide for Updating version and releasing With a Sample NodeJS Project

Photo by Caspar Camille Rubin on Unsplash

In the DevOps world, Applications are deployed into production very often. Some applications are deployed even on every hour to meet the customer needs. Versioning is very important before you release the app into production since if something happens to the current release all we need to rollback to the previous version of the app.

In this post, We will explore a little bit of SemVer and how we can do versioning with the npm tool for the nodejs projects.

  • What is SemVer
  • Example Project
  • How To Create a Release
  • Updating Patch Version
  • Updating Minor version
  • Updating Major version
  • Summary
  • Conclusion

What is SemVer

SemVer is versioning standard. It has three numbers separated by a dot and each number has its own meaning. The format for this standard is Major.Minor.Patch. We change the patch version whenever there is a fix in the release, for example, a bug. We change the minor version whenever there are changes in the release but with backward compatibility, for example, when we add methods or some more additional functionality. We change the major version when there are breaking changes without backward compatibility, for example, when we change the existing API or endpoints.

SemVer Versioning

Example Project

Here is an example project for this post. This is just a simple module that calculates the given two numbers.

// clone the project
git clone https://github.com/bbachi/calculate-module-semver.git
// run the module with this command
node -p "require('./')(3,4)"

-p flag evaluates the expression. This is the first release of the module and tagged this release as 1.0.0 on Github.

Initial Release

How To Create a Release

It’s always best practice to create a release or tag the specific release. This makes it easier to roll back the application to the previous version and debug the specific release. Check this link on how to create releases in GitHub.

Updating Patch Version

If we look at the index.js file there is a mistake in that file. There is a spelling mistake Calclate should be Calculate. Let’s fix this and update the patch version and release it.

var Calclate = function(a, b) {
    return a + b;
}
module.exports = Calclate;

All we need to run this command npm version patch. It will output the resulting version.

npm version patch

Let’s create a release 1.0.1 in the GitHub. You can see the releases here.

release 1.0.1

Updating Minor version

We have only one function which adds the given two numbers. Let’s change that to the function which takes the third parameter name of the function such as add, subtract, multiply. It would produce the output result based on the function name.

Let’s update the minor version number since we are not releasing any breaking changes. This works even without functionName because a default would be the addition a + b.

// with new changes
node -p "require('./')(3,6, 'multiply')"
18
// without functionName
node -p "require('./')(3,6)"
9

Let’s run this command npm version minor to update the minor version and release it.

version 1.1.0

Updating Major version

The previous change that we did was not great and error-prone. What happens when we mistype functionName when we call this module, it would always return addition operation. Let’s refactor the code so that we can methods on the module instead of passing parameters and using some latest features of javascript.

Let’s update the major version number since we are releasing it with breaking changes. This release doesn't work with the previous way anymore.

// doesn't work anymore
node -p "require('./')(3,6, 'multiply')"
18
// works with this release
node -p "require('./').multiply(7,3)"
21

Let’s run this command npm version major to update the major version and release this version.

npm version major
version 2.0.0

Summary

  • In the DevOps world, applications are released very often to meet customer needs.
  • It’s always best practice to tag the release. Tagging makes it easier to roll back the application to the previous version and debug the specific release.
  • SemVer is a versioning tool that consists of three numbers separated by a dot major.minor.patch
  • The major version can be updated with npm version major.
  • The minor version can be updated with npm version minor.
  • The patch version can be updated with npm version patch.

Conclusion

It’s always best practice to version your project and it makes it easier to update or rollback the deployment

JavaScript
Nodejs
Software Development
Programming
Web Development
Recommended from ReadMedium