About
version in a Javascript package can be found in the package.json file as property.
Syntax
Tag
Tags are a supplement to semver (e.g., v0.12) for organizing and labeling different versions of packages. In addition to being more human-readable, tags allow publishers to distribute their packages more effectively.
Version range Specification
This specification permits to upgrade the package by the package manager.
For example, to specify acceptable version ranges up to 1.0.4, use the following syntax:
- Patch releases: 1.0 or 1.0.x or ~1.0.4
- Minor releases: 1 or 1.x or ^1.0.4
- Major releases: * or x
Caret - increment the first non-zero portion of semver
To include everything that does not increment the first non-zero portion of semver, use the caret (aka hat) symbol, ^
Example:
- ^2.2.1
- ^0.1.0
- ^0.0.3
By default, npm adds your package using the caret operator in front of the version number (for example, ^3.5.2). It's recommend using the tilde operator instead (for example, ~3.5.2), which limits updates to the most recent patch-level version.
Tilde - particular version in the same minor range
To include everything greater than a particular version in the same minor range, use the tilde symbol, ~
example:
- ~2.2.0
Range
Range of stable versions
specify a range of stable versions use:
- >, <, =, >= or ⇐ for comparisons,
- or - to specify an inclusive range
examples:
- >2.1
- 1.0.0 - 1.2.0 - there must be spaces on either side of hyphens
Range of pre-release versions
specify a range of pre-release versions
use comparisons like > with a pre-release tag examples:
- >1.0.0-alpha
- >=1.0.0-rc.0 <1.0.1
OR: Multiple sets of versions
include multiple sets of versions use || to combine examples: ^2 <2.2 || > 2.3
Resolution
If there is:
- no package.json file in the root directory, the latest version of the package is installed.
- package.json file, the latest version satisfying the semver rule declared in package.json for that package (if there is any) is installed.
Management
Inject with WebPack
Read the version from Javascript Package - Package.json and inject it with the DefinePlugin in Webpack.
Example:
- Import the name
const webpack = require('webpack');
var packageJson = require('./package.json');
- Use the DefinePlugin and set the version in process.env.VERSION
var config = {
....
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
'process.env.VERSION': JSON.stringify(packageJson.version)
})
],
....
};
- In the code, the text process.env.VERSION will be replaced at compile time with the version.
export default myLib = {
version: process.env.VERSION;
}