Language: EN

gestion-de-versiones-en-npm

How to manage NPM package versions

Version management refers to the control we will exercise in our project over the dependency versions. Generally, dependencies will be third-party (from libraries, etc.) but it can also be from our own packages.

In the case of NPM, the packages we install and manage always have their designated version. NPM works with these versions to configure our project. Therefore, it is important to understand how to specify and work with these versions to manage our project.

Semantic Versioning

Semantic versioning is a convention used in version management that allows us to assign a version number following a specific format. This format consists of three numbers separated by dots, like this,

MAJOR.MINOR.PATCH
  • The number MAJOR: indicates a major change in the project that may cause incompatibilities with previous versions.
  • The number MINOR: indicates the addition of new functionalities to the project without altering its compatibility with previous versions.
  • The number PATCH: indicates the correction of bugs or minor issues without adding new functionalities or altering compatibility with previous versions.

Version Control in NPM

The NPM package manager provides us with tools to manage the versions of the dependencies of our projects easily.

Through the package.json file, we can specify the necessary dependencies for our project and the specific versions we want to use.

For example, if we want to use version 2.1.0 of a dependency called lodash, we can add the following line to the package.json file:

"dependencies": {
  "lodash": "2.1.0"
}

In this way, when we run the npm install command, NPM will install exactly version 2.1.0 of the lodash dependency in our project.

Version Ranges

In addition to specifying an exact version of a dependency, NPM allows us to use version ranges to indicate which versions are compatible with our project. Some examples of version ranges are:

  • ^2.1.0: indicates that any version greater than or equal to 2.1.0 and less than 3.0.0 is compatible.
  • ~2.1.0: indicates that any version greater than or equal to 2.1.0 and less than 2.2.0 is compatible.
  • >=2.1.0 <3.0.0: indicates that any version greater than or equal to 2.1.0 and less than 3.0.0 is compatible.

These version ranges allow us to specify our project’s dependencies more flexibly and take advantage of updates and improvements made in later versions.

Version Locking

In some cases, it may be necessary to lock the version of a dependency to prevent automatic updates. For this, we can use the = character followed by the specific version we want to lock.

For example, if we want to lock the version of the lodash dependency to version 2.1.0, we can add the following line to the package.json file:

"dependencies": {
  "lodash": "=2.1.0"
}

This ensures that the version of lodash will remain at version 2.1.0 as long as we do not explicitly modify this line in our package.json file.