Javascript Package - Version

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.

Caret

By default, npm adds your package using the caret operator in front of the version number (for example, ^3.5.2). We recommend using the tilde operator instead (for example, ~3.5.2), which limits updates to the most recent patch-level version.

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;
}

Powered by ComboStrap