How to read semantic versioning in npm

In npm, semantic versioning is a method for assigning version numbers to packages. In this guide I will show you how to read the versioning of packages in package.json.

Semantic versioning

The method of naming versions follows a pattern of 3 numbers: MAJOR.MINOR.PATCH. Every number has a different meaning when it gets updated.

  • MAJOR
    • Incremented when the package has breaking changes.
  • MINOR
    • Incremented when the package has new features and is backwards compatible.
  • PATCH
    • Incremented when package has only bugfixes.

package.json

In the package.json file, you can find lists of packages (= dependencies) that your project uses. Every package has a version number. The version number can be specified in different ways, so you can have control over which versions of the packages you want to use and gets installed.

See these examples of how you can specify a package version in package.json:

  • 7.8.2, =7.8.2
    • = MAJOR, = MINOR, = PATCH
    • This will install exactly version 7.8.2.
    • If there is a newer version available, it will not be installed.
  • ^7.8.2
    • = MAJOR, >= MINOR, >= PATCH
    • This will install the latest version that is compatible with 7.8.2.
    • For example, it will install 7.8.2, 7.8.3, 7.9.0, 7.9.1, but not 8.0.0.
  • ~7.8.2
    • = MAJOR, = MINOR, >= PATCH
    • This will install the latest version that is compatible with 7.8.2.
    • It will install 7.8.2, 7.8.3, 7.8.4, but not 7.9.0 or 8.0.0.
  • >=7.8.2
    • >= MAJOR, >= MINOR, >= PATCH
    • This will install the latest version that is greater than or equal to 7.8.2.
    • For example, it will install 7.8.2, 7.8.3, 7.9.0, 8.0.0, but not 7.7.0.
  • >7.8.2
    • > MAJOR, > MINOR, > PATCH
    • This will install the latest version that is greater than 7.8.2.
    • For example, it will install 7.8.3, 7.9.0, 8.0.0, but not 7.8.2 or 7.7.0.
  • <=7.8.2
    • <= MAJOR, <= MINOR, <= PATCH
    • This will install the latest version that is less than or equal to 7.8.2.
    • For example, it will install 7.8.2, 7.8.1, 7.7.0, but not 7.8.3 or 8.0.0.
  • <7.8.2
    • < MAJOR, < MINOR, < PATCH
    • This will install the latest version that is less than 7.8.2.
    • For example, it will install 7.8.1, 7.7.0, but not 7.8.2 or 7.8.3.
  • *, latest
    • Latest MAJOR, latest MINOR, latest PATCH
    • This will install the latest version available.

Update packages

To update all packages mentioned in the package.json you can run the CLI command: npm update. This command will also install missing packages. When you add a package name to that command (for example npm update package-name) it will only update that specific package. This command will respect the versioning as it is mentioned in the package.json file.

VS Code extension

I use the Package Json Upgrade extension in Visual Studio Code, which allows me to easily see the latest available versions of dependencies and install them with a single click.

package.json in VS Code

References

Semver.org - Semantic Versioning Specification

Github - npm version cheatsheet