After I upgraded to the latest stable node
and npm
, I tried npm install moment --save
. It saves the entry in the package.json
with the caret ^
prefix. Previously, it was a tilde ~
prefix.
npm
?~
and caret ^
?Instead of specifying the exact version to be installed in package. json, npm allows you to widen the range of accepted versions. You can allow a newer patch level version with tilde (~) and newer minor or patch level version with caret (^). Symbol. Dependency.
npm allows installing newer version of a package than the one specified. Using tilde ( ~ ) gives you bug fix releases and caret ( ^ ) gives you backwards-compatible new functionality as well. The problem is old versions usually don't receive bug fixes that much, so npm uses caret ( ^ ) as the default for --save .
The caret (aka hat) symbol, ^ , is used by default when you npm install --save a package. For example, npm install --save angular would add "angular": "^1.3. 15" to the dependencies in your package. json .
Peer dependencies are a special type of dependency that would only ever come up if you were publishing your own package. Having a peer dependency means that your package needs a dependency that is the same exact dependency as the person installing your package.
See the NPM docs and semver docs:
~version
“Approximately equivalent to version”, will update you to all future patch versions, without incrementing the minor version. ~1.2.3
will use releases from 1.2.3 to <1.3.0.
^version
“Compatible with version”, will update you to all future minor/patch versions, without incrementing the major version. ^2.3.4
will use releases from 2.3.4 to <3.0.0.
See Comments below for exceptions, in particular for pre-one versions, such as ^0.2.3
I would like to add the official npmjs documentation as well which describes all methods for version specificity including the ones referred to in the question
value | desc |
---|---|
~version | "Approximately equivalent to version" See npm semver - Tilde Ranges |
^version | "Compatible with version" See npm semver - Caret Ranges |
version | Must match version exactly |
>version | Must be greater than version |
>=version | etc |
<version | |
<=version | |
1.2.x | 1.2.0, 1.2.1, etc., but not 1.3.0 |
* | Matches any version |
latest | Obtains latest release |
The above list is not exhaustive. Other version specifiers include GitHub urls and GitHub user repo's, local paths and packages with specific npm tags
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With