The package.json file contains information about your Node.js project. One such piece of information is the core dependencies of the project. Core dependency is the package without which the project cannot operate properly. In the package.json file, you will find a list of packages along with their versions under the dependencies section.
If you take a look at the package.json file, you will notice one thing, just before the version number, you will find symbols like tilde, caret, etc. These symbols are used by NPM to decide which version of a package to install.
Node.js uses semantic versioning to version the packages. In semantic versioning, each version is represented by three numbers separated by dots.
Major.Minor.Patch
Note: To learn more about semantic versioning, visit What is Semantic Versioning in npm?
Caret (^) symbol: It fixes the major number, and incrementation is allowed in the minor and patch numbers. For example, ^4.8.2
means any version greater than or equal to 4.8.2 and less than 5.0.0 is valid.
Suppose you have a package.json file and it has the following content:
{ "name": "notes-app", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Mohit Natani", "license": "ISC", "dependencies": { "lodash": "^4.8.2" } }
When you run the npm install
command, npm will look for the latest minor version of the lodash
package in the NPM registry. On searching, it will find 4.17.21. So, npm will install version 4.17.21
of the lodash
package. You can verify this by running the npm list
command.
$npm list --depth=0 [email protected] /Users/mohitnatani/Desktop/nodejs-projects/notes-app |--- [email protected]
As you can see, npm has installed lodash version 4.17.21.
Tilde (~) symbol: It fixes the major and minor numbers, allowing incrementation in the patch number. For example, ~4.16.2
means any version greater than or equal to 4.16.2 and less than 4.17.0 is valid.
Suppose, instead of the caret (^) symbol, the tilde (~) symbol is present for the lodash
package in the package.json
file.
{ "name": "notes-app", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Mohit Natani", "license": "ISC", "dependencies": { "lodash": "~4.16.2" } }
When you run the npm install
command, NPM will look for the latest patch version less than 4.17.0 in the NPM registry. On searching, it will find 4.16.6. so, NPM will install 4.16.6 on your computer. You can verify this by running the npm list
command.
$npm list --depth=0 [email protected] /Users/mohitnatani/Desktop/nodejs-projects/notes-app |--- [email protected]
It's time to look at the difference between tilde (~) and caret (^) in package.json.
Tilde (~) Notation | Caret (^) Notation |
---|---|
NPM does not use tilde as the default notation in package.json. | Caret (^) is used by NPM as the default notation in package.json. |
Tilde provides bug fixes. | Caret provides a backward compatible update of the package. |
Tilde updates the package to the latest patch version. | Caret updates the package to the latest minor/patch version. |
In tilde notation, an increment in the patch number is allowed. | In caret notation, increments in both minor and patch numbers are allowed. |