a blog for those who code

Friday 14 October 2016

Difference between Tilde and Caret in package.json of Nodejs

In this post we will be discussing about the difference between Caret and Tilde in package.json of Node.js. Package.json holds various metadata relevant to the project to identify the project and the dependencies. This file also helps in managing the dependencies for the node.js project.

Inside the package.json file we have the dependencies field which is used to list all the dependencies of the Node.js project that are available on npm. When you run npm install, all the dependencies listed in the package.json will be installed.

Now to specify the version you have different options with you (characters in front of version) as shown below

"dependencies": {
  "package1": "~0.2.0",
  "package1": "^0.2.0",
  "package1": "0.2.0",
} 

So you might be wondering that why exactly tilde(~) or caret(^) is out there in package.json and what is there significance. So basically they are used to do version control in an application thus indicating the restriction of the package version used as a dependency.

Tilde (~) : It will allow patch level changes if a minor version is specified. It means that if you say ~1.2.3 it will allow version from 1.2.3 to 1.3.0 but will skip 1.3.0. In simple terms it matches the right most version segment to increment above the given value.

Caret (^) : It will allow changes that do not modify left-most non-zero digits. It means that if you have ^1.2.3 it will match any 1.x.x release but will not include 2.0.0.  Do note that it does not supported in older version of npm i.e. from 1.4.2 on-wards it is supported.

These are important because you need not have to change your versions every-time there is a release or version update of your npm package.

Please Like and Share the CodingDefined.com Blog, if you find it interesting and helpful.

No comments:

Post a Comment