Version management refers to the control of changes made to the code over time. This involves keeping a record of all previous versions of the project, as well as its dependencies.
In the case of NPM, the packages we install and manage will have their version assigned. NPM works with these versions to configure our project.
Therefore, it is important to understand how to reference and work with these versions, to manage our project’s dependencies.
Semantic versioning
Semantic versioning is a convention used in version management that allows us to assign a version number to our project following a specific format. This format consists of three numbers separated by dots: MAJOR.MINOR.PATCH.
- The MAJOR number indicates a major change in the project that can cause incompatibilities with previous versions.
- The MINOR number indicates the addition of new features to the project without altering its compatibility with previous versions.
- The PATCH number indicates the correction of minor errors or problems without adding new features or altering compatibility with previous versions.
What is versioning in programming
Version control in NPM
The NPM package manager provides us with tools to manage the versions of our project’s dependencies in a simple way.
Through the package.json
file, we can specify the dependencies needed 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 the dependencies of our project in a more flexible way and take advantage of the updates and improvements made in subsequent versions.
Version locking
In some cases, it may be necessary to lock the version of a dependency to prevent automatic updates. To do 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 lodash
version will always remain at version 2.1.0, as long as we do not explicitly modify this line in our package.json
file.