How to increase the SemVer of your NodeJs package?

Kat Lim Ruiz
2 min readMar 13, 2020

Every team or developer that publishes a package to npm has to deal with the versioning of it. It is part of the package lifecycle management and, either a company, a team or a single developer, it demonstrates how serious and professional you are towards your users (which are the developers using your package).

Today the trend in versioning schemes is to follow what is called Semantic Versioning, or short SemVer.

SemVer has the format Major.Minor.Patch(-Label) .

  • Major: for breaking changes.
  • Minor: for medium to big modifications to the component, yet keeping its backwards compatibility.
  • Patch: for small modifications or bug fixing.
  • Label: optional component to indicate pre-release.

Normally packages should start with 0.0.1, and then increase for every change or release based on the guide above.

NodeJs offers a small yet very useful command to increase the versioning of your package.

Once you are ready to commit your changes and publish to npm, you can use the command:

npm version patch

to increase the patch component of your version. For example, to go from 0.0.0 to 0.0.1.

Using npm version minor will increase to 0.1.1 and npm version major will increase to 1.1.1.

This command is only allowed to when there are no pending commits. And interestingly, when you execute it, it will make the change to the package.json file and it will commit it to the repo, with an automatic message.

Again, really cool implementation as it provides an standard mechanism that we can use to execute this task that can be cumbersome at times.

A little story (so no one makes the same mistake)

Whenever I created a nodejs package and I wanted to publish to npm, I followed the theory and committed the package.json and package-lock.json files as part of the repo. When the package is installed at the hosting application, npm uses the lock file to make it consistent.

And so when it was time to publish, the version had to be increased in both package.json and package-lock.json. I did this manually (yes, I know). This obviously would be error prone and I have many commits where includes one file first, and a second commit to update the lock file.

NOTE: if both files are out of sync, your package can behave unexpectedly.

After some months, I discovered that if I ran npm install, the lock file would be updated too so I thought “that is cool”.

It is later on when I found about npm version command. This command would run and update both files at the same time “This is really cool!”.

Ironically after just some weeks, during some implementation, I found out that package-lock.json was not needed for components, and actually it is a bad practice to publish it (TODO: find the link).

I was falling into a problem that it was not even needed (:facepalm).

Hope this helps to anyone reading.

Happy coding!

--

--

Kat Lim Ruiz

Software Engineer, father, technology enthusiast, agilist, INTJ, Developer, Mini-Devops.