Node Package Manager Tutorial What's The Semantic Versioning And How To Control It Also From The Package.Json File
What Will I Learn?
I will learn how to control the versions of the package and how to install from the package.json file.
- What's The Semantic Versioning .
- How to control the versions of the package and install a specific version .
- How to install from package.json file .
Requirements
- Must downloaded the node js .
- Basic knowledge about the command line .
- Basic knowledge about modules in Node Js.
Difficulty
- Basic
Tutorial Contents
In this tutorial am going to talk about the npm versioning about the versions of the packages and how to install it from the package.json file , so let's start ..
Most of web developers working on building a packages to help us to reuse their codes to resolve our problems , they upload the package to be on the registry of the npm .
Firstly when you install the package locally you will find your package in your ' package.json ' file like it
I have installed my moment package which is a date library as I mentioned in the last tutorial I installed it locally by the npm install moment --save
and this is the package.json file
The package.json contains all the informations about my modules and packages it has the packages installed locally as a dependencies and dev dependencies as I explained in the last tutorial , but if you look here in this package that I installed
Each package has a version , when a developer create a package and upload it in the npm registry it will have by default the version 1.0.0 which is the first version of this package , the version has 3 numbers and this is what are mean ..
I have in my picture 5 elements let's explain them
The first element is the name of my module or package which is the name that was enregistred on the npm registry , by the name you can install , uninstall and control it .
The second element is the notation for version range there is almost two characters , when we install the package locally in the package.json file we can find this notation :
- ~ character : this character means that the version includes all patch versions for example if we have this version "~5.4.3" it will includes the versions from ' 5.4.3 to 5.5.0 ' but the second minor will not inserted .
- ^ character : this character means that the version includes all patch and minor versions if we have the version "^5.4.3" it will includes the versions from '5.4.3 to 6.0.0 ' .
But firstly what are the minor , major and patch ?
Patch Version Release
This is the last part it starts with 0 and if there is an update in text for example images , fix bugs or something will not break the functionalities of the application will increment the digit by 1 to be for example 1.0.1 .
Minor Version Release
This is the middle number it changes if there is a modification a new features created or some changes in the functionalities to add more options without breaking existing features , for example 1.1.0 .
Major Version Release
This is the first number if there is a full modification a new features that break a existing functionalities that break the project that you are working on , like for example angular 1 to angular 2 , there is many changes and modifications that will certainly make a problems in your project .
So it's really important to understand what's version you installed with your packages , because when you install the package it will install in the last version , sometimes we need a specific version of package less than the last version to install a specific version you need to add this sign ' @ ' followed by the version like it npm install moment@2.1.0
and this is the result
I installed the moment library locally now and I specified the version 2.1.0 , let's see what's happened inside the package.json
As you have seen the version is 2.1.0 now as I specified when I have installed it .
If you want to fix the major and minor versions for example and take the patch free let's see what's happened
I used the npm install moment@2.21 --save
the sign ' @ ' as I mentioned above to select the version and I fixed the first and second numbers , now the npm will give us the last version or the last patch which is here 0 so from 2.21.0 to 2.22.0 there is a changes and updates in a existing functionalities , let's see our package.json file
This is the version that I installed right now , if I want to see what's the last version in the major 1 of this package I will just fix the first number to 1 like it npm install moment@1 --save
So the last version of this library when the major 1 is ' 1.7.2 ' let's see what's happened in our package.json file
If you look at the character ' ^ ' it means that there is other patch and minor versions includes of this packages as I explained before .
Installing from package.json
If you are working on a project and you need to install some packages , there is different ways I have explained the simple way to install the package from the command line by this code npm install package --save
to be locally as a dependency on your local project and if you look at the package.json you can find it in the dependencies array .
Now I want to give you another method to install and to control the versions from the package.json file , let's open our folder firsly
The folder is empty now , I have just the package.json file and inside it I will add the moment with the last version inside the dependencies array like it
I will now type the code npm install
and see what's happened
So like it I have installed the last version of moment package from my package.json file and when I ran it from the command line it has created a folder named ' node_modules ' has the moment package and this is the result
As we learned about the versioning let's see how to specify the version from the package.json file
So I selected the version 2.20.0 and by the npm install command it did an update in my package
Now if I change the type of the character like it ' ~ ' and when I select the version for example ' 2.20.0 ' it will install the last patch in this version , this is my package.json file
Now I will run it by the command line it will install the last version as I mentioned before
And if you want to fix the version of the package you must remove the character and keep the version only the major,minor and the patch to be like it
And like it the version will be fixed , if you want to install the last version just make an ' * ' like it
It will install the last version of this package by this ' * ' sign .
Curriculum
Posted on Utopian.io - Rewarding Open Source Contributors
https://www.youtube.com/paulbegley34
Thank you for the contribution It has been approved.
Need help? Write a ticket on https://support.utopian.io.
Chat with us on Discord.
[utopian-moderator]
Thank you sir
Hey @alexendre-maxim I am @utopian-io. I have just upvoted you!
Achievements
Utopian Witness!
Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
Thank you